+ 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.
4 Antworten
+ 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));
}
}
+ 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
+ 1
@Dayve Ooh great! Thank you so much, this makes things look much nicer.
+ 1
@ChaoticDawg Many thanks for the info! This method is pretty interesting, and looks simple to use too!