Java Equivalent to .NET's String.Format

后端 未结 6 1428
故里飘歌
故里飘歌 2020-12-05 17:09

Is there an equivalent to .NET\'s String.Format in Java?

6条回答
  •  失恋的感觉
    2020-12-05 17:44

    You can also simply use %s for string since the index is an optionnal argument.

    String name = "Jon";
    int age = 26;
    String.format("%s is %s years old.", name, age);
    

    It's less noisy.

    Note about %s from the java documentation:

    If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().

提交回复
热议问题