Java 等价于 Python 的 format()

标签 java string

这里有两种进行字符串替换的方法:

name = "Tshepang"
"my name is {}".format(name)
"my name is " + name

如何使用 Java 执行类似于第一种方法的操作?

最佳答案

name = "Paŭlo";
MessageFormat f = new MessageFormat("my name is {0}");
f.format(new Object[]{name});

或更短:

MessageFormat.format("my name is {0}", name);

关于Java 等价于 Python 的 format(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5324007/

相关文章:

java - 通过apache POI读取java中的.xlsx

java - 使用双引号生成的字符串未在字符串池中生成

string - 不区分大小写的 NSString 比较

java - iframe 不显示 struts 操作类中的对象

java - 如何在 Java 8 中使用 reduce 从对象列表创建字符串列表?

java - SAX 解析 : Encountered mixed content within text element

c - 编程查找总和,如果输入 'y',则使用 do while 循环重复该过程

java - 可运行类未执行

javascript - 正则表达式替换字符串中不是数字或句点的任何内容

java - 将 int 传递给 Android 应用程序的 .xml