+ 2

Does Java have anything similar to this formatting option? (C#)

In C# it is possible to format using something like ("{0} and {1}", value1, value2) where value1 and value2 replace {0} and {1} respectively. Is anything similar to this available in Java? Thank you.

10th Aug 2017, 3:57 PM
Shannon
Shannon - avatar
4 Respostas
+ 8
You can use the JavaSE's String.format class which Returns a formatted string using the specified format string and arguments. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29 Example : public class Program { public static void main(String[] args) { int x = 4, y = 5; System.out.println(String.format("%d + %d = %d", x, y, x + y)); } }
10th Aug 2017, 4:16 PM
Dev
Dev - avatar
+ 2
Java has String.format() Which is similar to printf and java.text.MessageFormat which is closer to what you're looking for. https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
10th Aug 2017, 4:21 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@Dayve Ooh great! Thank you so much, this makes things look much nicer.
10th Aug 2017, 4:18 PM
Shannon
Shannon - avatar
+ 1
@ChaoticDawg Many thanks for the info! This method is pretty interesting, and looks simple to use too!
10th Aug 2017, 4:27 PM
Shannon
Shannon - avatar