How can we use java String format in Scala

0 votes
I am using .format method to format a string. How can I do it in Scala?
May 29, 2018 in Java by Sushmita
• 6,920 points
1,659 views

3 answers to this question.

0 votes
val placeholderString = "Hello %s, isn't %s cool?"
val formattedString = placeholderString.format("Ivan", "Scala")
answered May 29, 2018 by sharth
• 3,370 points
0 votes
// we can specify the # of decimals we want to show for a floating point:
String result = String.format("10 / 3 = %.2f", 10.0 / 3.0);
// result now equals  "10 / 3 = 3.33"

// we can add commas to long numbers:
result = String.format("Today we processed %,d transactions.", 1000000);
// result now equals  "Today we processed 1,000,000 transactions."
answered Jul 23, 2018 by Parth
• 4,640 points
0 votes
String aString = "world";
int aInt = 20;
String.format("Hello, %s on line %d",  aString, aInt );

Output will be:

Hello, world on line 20
String.format("Line:%2$d. Value:%1$s. Result: Hello %1$s at line %2$d", aString, aInt );

Output will be:

Line:20. Value:world. Result: Hello world at line 20

answered Aug 29, 2018 by Daisy
• 8,140 points

Related Questions In Java

0 votes
1 answer

How can we split a comma separated String in Java?

String str = "..."; // write the ...READ MORE

answered Jun 12, 2018 in Java by Parth
• 4,640 points
2,327 views
0 votes
1 answer

How can we use wait and notify in Java?

You can refer to this: synchronized (someObject) { ...READ MORE

answered Jul 12, 2018 in Java by sophia
• 1,400 points
662 views
0 votes
1 answer

How can we encrypt a String in Java?

Let's assume the bytes to encrypt are ...READ MORE

answered Oct 26, 2018 in Java by Sushmita
• 6,920 points
1,397 views
0 votes
6 answers

How can we define global variables in java?

To define Global Variable you can make ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,890 points
117,394 views
0 votes
2 answers

Result of character addition in Java

Binary arithmetic operations on char and byte ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
4,580 views
0 votes
2 answers

How can we Split Java String by New Line?

If you don’t want empty lines: String.split("[\\r\\n]+") READ MORE

answered Aug 22, 2018 in Java by Daisy
• 8,140 points
7,075 views
0 votes
1 answer

How can we nicely format a floating number into String by removing unnecessary zeros?

public static String fmt(double d) { if(d ...READ MORE

answered Jun 6, 2018 in Java by Parth
• 4,640 points
740 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
1,139 views
0 votes
1 answer

Can we use multiline String in Java?

It is not possible directly to use Multiline ...READ MORE

answered Jun 19, 2018 in Java by sharth
• 3,370 points
723 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,211 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP