字符串替换问题

发布于 2024-11-19 01:14:22 字数 320 浏览 6 评论 0原文

我拥有的:

我有一个文本“嗨{0},我的名字是{1}。”

我有一个List; name = Arrays.asList("Peter", "Josh");

我正在尝试将 Peter 放在有 {0} 的地方,将 Josh 放在有 {1} 的地方。

我想要什么:

Hi Peter, my name is Josh.

我有什么想法可以做到吗?

What I have:

I've got a text "Hi {0}, my name is {1}."

I've got a List<String> names = Arrays.asList("Peter", "Josh");

I'm trying to fit Peter where there's a {0} and Josh where there's a {1}.

What I want:

Hi Peter, my name is Josh.

Any ideas of how could I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

牵你的手,一向走下去 2024-11-26 01:14:22

MessageFormat 类是你的朋友。 http://download.oracle.com/javase /1.5.0/docs/api/java/text/MessageFormat.html

                String aa = "Hi {0}, my name is {1}";

            Object[] bb = {"Peter" , "John"};

            System.out.println(MessageFormat.format(aa, bb));

MessageFormat class is your friend. http://download.oracle.com/javase/1.5.0/docs/api/java/text/MessageFormat.html

                String aa = "Hi {0}, my name is {1}";

            Object[] bb = {"Peter" , "John"};

            System.out.println(MessageFormat.format(aa, bb));
沫雨熙 2024-11-26 01:14:22

最简单的可能是在循环中使用 String.replaceXX 操作之一。例如,

String sourceString = "Hi {1}, my name is {2}."
for (i = 0; i < names.size(); i++) {
    String repText = names.get(i);
    sourceString = sourceString.replace("{" + (i+1) + "}", repText);
}

这有点低效,因为与使用 StringBuffer 或类似的东西相比,重复创建新字符串是一种不好的形式,但通常这种形式的文本替换是低频操作,因此简单性胜过效率。

Probably simplest would be to use one of the String.replaceXX ops in a loop. Eg,

String sourceString = "Hi {1}, my name is {2}."
for (i = 0; i < names.size(); i++) {
    String repText = names.get(i);
    sourceString = sourceString.replace("{" + (i+1) + "}", repText);
}

This is a bit inefficient, since it's bad form to repeatedly create new Strings vs using a StringBuffer or some such, but generally text replacement of this form would be a low-frequency operation, so simplicity trumps efficiency.

姜生凉生 2024-11-26 01:14:22
List<String> names = new ArrayList<String();
names.add("Peter");
names.add("Josh");
String str = "Hi {1}, my name is {2}.";
str = str.replaceFirst("{1}", names.get(0));
str = str.replaceFirst("{2}", names.get(1));
List<String> names = new ArrayList<String();
names.add("Peter");
names.add("Josh");
String str = "Hi {1}, my name is {2}.";
str = str.replaceFirst("{1}", names.get(0));
str = str.replaceFirst("{2}", names.get(1));
小梨窩很甜 2024-11-26 01:14:22
String text = "Hi {1}, my name is {2}.";
java.util.List<String> names = Arrays.asList("Peter", "Josh");
for(String s: names) text = text.replace("{" + (names.indexOf(s) + 1) + "}", s);
String text = "Hi {1}, my name is {2}.";
java.util.List<String> names = Arrays.asList("Peter", "Josh");
for(String s: names) text = text.replace("{" + (names.indexOf(s) + 1) + "}", s);
无人接听 2024-11-26 01:14:22

你会做这样的事情。

List<String> names = Arrays.asList("Peter", "Josh");
System.out.printf("Hi %s, my name is %s.", names.get(0), names.get(1));

只需 2 行代码即可完成。

You would do something like this.

List<String> names = Arrays.asList("Peter", "Josh");
System.out.printf("Hi %s, my name is %s.", names.get(0), names.get(1));

and that would be it in just 2 lines of code.

森末i 2024-11-26 01:14:22
List<String> names = new ArrayList<String>();
names.add("Peter");
names.add("Josh");
System.out.println("Hi " + names.get(0) + ", my name is " + names.get(1) + ".");

如果我太从字面上理解你并且你想要一些更通用的东西,我很抱歉,但这将完全按照你的要求进行。

List<String> names = new ArrayList<String>();
names.add("Peter");
names.add("Josh");
System.out.println("Hi " + names.get(0) + ", my name is " + names.get(1) + ".");

My apologies if I'm taking you too literally and you wanat something more generic but this will do exactly as you asked.

﹉夏雨初晴づ 2024-11-26 01:14:22

我假设您的列表将具有正确数量的元素。

`String s = "Hi {1}, my name is {2}.";`
for(int x = 1;x <= names.size();x++)
{
   s.replaceFirst("{" + x +"}",names.get(x - 1));
}

I'm assuming that your list will have the correct number of elements.

`String s = "Hi {1}, my name is {2}.";`
for(int x = 1;x <= names.size();x++)
{
   s.replaceFirst("{" + x +"}",names.get(x - 1));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文