I have a template string
str = "Hello %s, your name is %s, %s 's born in 1990."
Run Code Online (Sandbox Code Playgroud)
I want to format this string, as below: Hello Mr.P, your name is Mr.P, Mr.P 's born in 1990. I can do the following below:
String.format(str, "Mr.P","Mr.P","Mr.P");
Run Code Online (Sandbox Code Playgroud)
产生以上。还有什么其他方式可以格式化字符串,而不必重复与n参数相同的值次数
您可以简单地引用一个特定的参数:
String.format("Hello %1$s, your name is %1$s", "Mr.P")
Run Code Online (Sandbox Code Playgroud)
% =格式字符串的开头
1$ =第一个论点
s =类型字符串
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |