Java: does String.Format exist in Java, e.g. in C #?

Something I discovered I like in C # - properties and String.Format.

Is there something like String.Format from C # in Java?

C # ex:

int myNum = 2; 
int myNumSq = myNum * myNum;
String MyString = String.Format("Your lucky numbers are: {0}, & {1}", myNum, myNumSq); 
+5
source share
3 answers

It is even called String.format () :

String myString = String.format("Your lucky numbers are: %d, & %d", myNum, myNumSq);

This method is available since Java 1.5.

+4
source

Yes, the class in question is "MessageFormat":

http://download.oracle.com/javase/6/docs/api/java/text/MessageFormat.html

MessageFormat.Format("Your lucky numbers are: {0}, & {1}", myNum, myNumSq);

(Not sure if automatic boxing will work correctly in this case - you may need to convert intto first Integer)

+6
source
String myString = String.format("Your lucky numbers are: %d, & %d", myNum, myNumSq);
+2
source

All Articles