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,880 points
212 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,320 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 parth295
• 4,620 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,050 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 parth295
• 4,620 points
56 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
26 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,880 points
17 views
0 votes
4 answers

How can we define global variables in java?

class G { static String globalVar ...READ MORE

answered Jan 29 in Java by zemiak
3,775 views
0 votes
2 answers

Result of character addition in Java

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

answered Aug 22 in Java by Sirajul
• 25,700 points
49 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,050 points
395 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 parth295
• 4,620 points
32 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,540 points
46 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,320 points
82 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 in Java by Sirajul
• 25,700 points
133 views