java 中的字符串格式化以用指向的字符串元素替换数组索引
String Formatting in java to replace the array index with the pointed string element
我们要替换如下 阿里巴巴网站china comparison best-Replacement array
{} 是 {4} 在线 {3} 购物 {} 在 {}-positional argument array
输出必须是 Alibaba is best online comparison shopping site in China.Please help me in getting so
您需要使用 MessageFormat 并附加您想要创建消息的参数..
示例:
public static void main(String[] args) {
String msgRaw = "Alibaba {0} {1} site {2} China";
String[] appender = { "is the best", "online comparison shopping", "in" };
String text = MessageFormat.format(msgRaw, appender);
System.out.println(text);
}
输出
Alibaba is the best online comparison shopping site in China
您可以像这样使用 MessageFormat
public static void main(String args[]) throws Exception{
String result = MessageFormat.format("{0} is {4} online {3} shopping {1} in {2}" ,
"Alibaba" ,"site", "china", "comparison", "best");
System.out.println(result);
}
它会输出
Alibaba is best online comparison shopping site in china
我们要替换如下 阿里巴巴网站china comparison best-Replacement array {} 是 {4} 在线 {3} 购物 {} 在 {}-positional argument array
输出必须是 Alibaba is best online comparison shopping site in China.Please help me in getting so
您需要使用 MessageFormat 并附加您想要创建消息的参数..
示例:
public static void main(String[] args) {
String msgRaw = "Alibaba {0} {1} site {2} China";
String[] appender = { "is the best", "online comparison shopping", "in" };
String text = MessageFormat.format(msgRaw, appender);
System.out.println(text);
}
输出
Alibaba is the best online comparison shopping site in China
您可以像这样使用 MessageFormat
public static void main(String args[]) throws Exception{
String result = MessageFormat.format("{0} is {4} online {3} shopping {1} in {2}" ,
"Alibaba" ,"site", "china", "comparison", "best");
System.out.println(result);
}
它会输出
Alibaba is best online comparison shopping site in china