Java 用其他东西替换花括号内的内容(例如 {1})
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24256469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing contents inside curly braces (for e.g. {1}) with something else
提问by Smrita
I have a string as follows
我有一个字符串如下
Hey {1}, you are {2}.
Here 1
and 2
are key whose value will be added dynamically.
这里1
和2
是其值将被动态添加的键。
Now I need to replace {1}
with the value that 1
represents and then I need to replace {2}
with the value that 2
represents in the sentence above.
现在我需要{1}
用1
代表的值替换,然后我需要{2}
用2
上面句子中代表的值替换。
How do I do it?
我该怎么做?
I know what split function of a string does and I know very well that with that function I can do what I want but I am looking for something better.
我知道字符串的 split 函数有什么作用,我很清楚使用该函数我可以做我想做的事,但我正在寻找更好的东西。
Note:I don't know what these keys are in advance. I need to retrieve the keys as well. And then according to the keys I need to replace the values in the string.
注意:我事先不知道这些键是什么。我也需要检索密钥。然后根据我需要替换字符串中的值的键。
采纳答案by TheLostMind
Thanks to https://stackoverflow.com/users/548225/anubhavafor this one.. :). You could do something like this:
感谢https://stackoverflow.com/users/548225/anubhava为这个.. :)。你可以这样做:
public static void main(String[] args) {
String s = "Hey {1}, you are {2}.";
HashMap<Integer, String> hm = new HashMap();
hm.put(1, "one");
hm.put(2, "two");
Pattern p = Pattern.compile("(\{\d+\})");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
String val1 = m.group().replace("{", "").replace("}", "");
System.out.println(val1);
s = (s.replace(m.group(), hm.get(Integer.parseInt(val1))));
System.out.println(s);
}
}
Output:
输出:
Hey one, you are two.
回答by Pablo Francisco Pérez Hidalgo
Try using String.format()
:
尝试使用String.format()
:
String x = String.format("Hi %s, you are %s\n", str1, str2);
If you have already a string "Hey {1}, you are {2}."
you can use regular expressions to replace {1}
and {2}
with %s
.
如果你已经有一个字符串"Hey {1}, you are {2}."
,你可以使用正则表达式替换{1}
和{2}
使用%s
。
String template = "Hey {1}, you are {2}.";
String x = String.format(template.replaceAll("\{\d\}", "%s"), "Name", "Surname");
回答by Anoop George
Try this:
尝试这个:
String str = "Hey {1}, you are {2}.";
str = str.replaceFirst("\{.*?\}", "Matt");
Output:
输出:
Hey Matt, you are {2}.
The above code will replace the first instance of {1}
. If you call this sequence in a loop replacing with the key values from your code, it'll replace all strings within {...}
in the order mentioned in the given string.
上面的代码将替换{1}
. 如果您在循环中调用此序列替换代码中的键值,它将{...}
按照给定字符串中提到的顺序替换其中的所有字符串。
回答by amit bhardwaj
Try this i think this will work from what i understand from your question
试试这个我认为这会根据我从你的问题中理解
public static void main(String[] args) {
String str = "Hey {1} your {2} is {3} clear to {4} ";
Map<Integer,String> map = new LinkedHashMap<Integer,String>();
map.put(1, "Smrita");
map.put(2, "question");
map.put(3, "not");
map.put(4, "anyone");
while(str.contains("{")){
Integer i = Integer.parseInt(str.substring(str.indexOf("{")+1, str.indexOf("{")+2));
str = str.replace("{"+i+"}", map.get(i));
}
System.out.println(str);
}
the output is
输出是
Hey Smrita your question is not clear to anyone
Hey Smrita your question is not clear to anyone
回答by Pranav Maniar
You can use MessageFormatfrom java.text.MessageFormat. Message Formathas some example on how it can be used in this type of scenario
您可以使用java.text.MessageFormat 中的MessageFormat。 Message Format有一些关于如何在此类场景中使用的示例