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,840 points
173 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,600 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,040 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,600 points
44 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
18 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,840 points
13 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
2,274 views
0 votes
1 answer

Result of character addition in Java

The result of adding Java chars, shorts, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,520 points
40 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,040 points
240 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,600 points
26 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,520 points
37 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
70 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
83 views