Jam*_*s T 1 java string replace
我在代码中看到过这样的东西
somestring = "Today is {0}, tomorrow is {1}";
Run Code Online (Sandbox Code Playgroud)
我知道它会将值放入另一个变量的字符串中,但我该怎么做呢?
更新:由于有几种方法可以达到这种效果,哪种方式最有效?
你看过MessageFormat文档了吗?
例如(一个例子)
Object[] arguments = {
new Integer(7),
new Date(System.currentTimeMillis()),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
arguments);
output: At 12:30 PM on Jul 3, 2053, there was a disturbance
in the Force on planet 7.
Run Code Online (Sandbox Code Playgroud)
关于你的问题.效率,如果遇到性能问题,我只会担心这个问题.我希望上述内容合理有效 - 特别是考虑到可能会有一些输出(标准输出或类似?),我认为这是一个更大的瓶颈.
// Assuming variables today and tomorrow are strings.
somestring = String.format("Today is %s, tomorrow is %s", today, tomorrow);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
228 次 |
| 最近记录: |