【发布时间】:2013-12-03 01:49:53
【问题描述】:
我有以下程序,我想用对应的值替换所有出现的字符串,其中单词作为键存在于地图中。
我已经实现了 4 种方法。它们中的每一个都执行大致相同的功能,但方式不同。前 3 个的输出不正确,因为下一个替换会覆盖前一个的结果。第四个有效,但只是因为我要替换整个字符串中的单个字符。这无论如何都是非常低效的,因为我只检查整个字符串的一个子字符串。
有没有办法安全地替换所有匹配项而不覆盖以前的替换项?
我注意到 Apache 有一个 StringUtils.replaceEach() 方法,但我更喜欢使用地图。
输出:
Apple BApplenApplenApple CApplentApplelope DApplete Apple BApplenApplenApple CApplentApplelope DApplete
Apple BApplenApplenApple CApplentApplelope DApplete Apple BApplenApplenApple CApplentApplelope DApplete
Apple BApplenApplenApple CApplentApplelope DApplete Apple BApplenApplenApple CApplentApplelope DApplete
Apple Banana Cantalope Date Apple Banana Cantalope Date
ReplaceMap.java
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReplaceMap {
private static Map<String, String> replacements;
static {
replacements = new HashMap<String, String>();
replacements.put("a", "Apple");
replacements.put("b", "Banana");
replacements.put("c", "Cantalope");
replacements.put("d", "Date");
}
public ReplaceMap() {
String phrase = "a b c d a b c d";
System.out.println(mapReplaceAll1(phrase, replacements));
System.out.println(mapReplaceAll2(phrase, replacements));
System.out.println(mapReplaceAll3(phrase, replacements));
System.out.println(mapReplaceAll4(phrase, replacements));
}
public String mapReplaceAll1(String str, Map<String, String> replacements) {
for (Map.Entry<String, String> entry : replacements.entrySet()) {
str = str.replaceAll(entry.getKey(), entry.getValue());
}
return str;
}
public String mapReplaceAll2(String str, Map<String, String> replacements) {
for (String key : replacements.keySet()) {
str = str.replaceAll(Pattern.quote(key),
Matcher.quoteReplacement(replacements.get(key)));
}
return str;
}
public String mapReplaceAll3(String str, Map<String, String> replacements) {
String regex = new StringBuilder("(")
.append(join(replacements.keySet(), "|")).append(")").toString();
Matcher matcher = Pattern.compile(regex).matcher(str);
while (matcher.find()) {
str = str.replaceAll(Pattern.quote(matcher.group(1)),
Matcher.quoteReplacement(replacements.get(matcher.group(1))));
}
return str;
}
public String mapReplaceAll4(String str, Map<String, String> replacements) {
StringBuilder buffer = new StringBuilder();
String regex = new StringBuilder("(")
.append(join(replacements.keySet(), "|")).append(")").toString();
Pattern pattern = Pattern.compile(regex);
for (int i = 0, j = 1; i < str.length(); i++, j++) {
String s = str.substring(i, j);
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
buffer.append(s.replaceAll(Pattern.quote(matcher.group(1)),
Matcher.quoteReplacement(replacements.get(matcher.group(1)))));
} else {
buffer.append(s);
}
}
return buffer.toString();
}
public static String join(Collection<String> s, String delimiter) {
StringBuilder buffer = new StringBuilder();
Iterator<String> iter = s.iterator();
while (iter.hasNext()) {
buffer.append(iter.next());
if (iter.hasNext()) {
buffer.append(delimiter);
}
}
return buffer.toString();
}
public static void main(String[] args) {
new ReplaceMap();
}
}
【问题讨论】:
-
我在这里得到了一些非常有用的答案:stackoverflow.com/questions/26735276/…