There are two types of format strings in Java,
String.format()
similar to the printf()
;
MessageFormat.format()
more suitable for message type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.text.MessageFormat; import java.util.Date;
public class Main { public static void main(String[] args) { String stringFormat = "lexical error at position %s, encountered %s, expected %s ";
String messageFormat = "lexical error at position {0}, encountered {1}, expected {2}";
System.out.println(String.format(stringFormat, 123, 100, 456));
System.out.println(MessageFormat.format(messageFormat, new Date(), 100, 456)); } }
|
1 2
| lexical error at position 123, encountered 100, expected 456 lexical error at position 7/28/22 11:31 PM, encountered 100, expected 456
|