Java: Does String.Format exist in Java like in C#?

前端 未结 3 994
旧巷少年郎
旧巷少年郎 2021-01-13 06:28

Something I discovered I like about C# are properties & String.Format.

Does something like String.Format from C# exist in Java?

C# ex:



        
相关标签:
3条回答
  • 2021-01-13 07:00
    String myString = String.format("Your lucky numbers are: %d, & %d", myNum, myNumSq);
    
    0 讨论(0)
  • 2021-01-13 07:03

    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 auto-boxing will work correctly in this case - you might need to convert the int to an Integer first)

    0 讨论(0)
  • 2021-01-13 07:15

    It' even called String.format():

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

    This method is available since Java 1.5.

    0 讨论(0)
提交回复
热议问题