我有以下计划,我想更换其中一个词存在,如为相应的值映射中的关键字字符串的所有地方。
我已经实现4种方法。 他们每个人都执行大致相同的功能,但以不同的方式。 用于第一3的输出是不正确的作为下一个替换覆盖以前的结果。 第四个作品,但只因为我整个字符串中更换单个字符。 这是非常低效的,反正,因为我只检查了整个字符串的子串。
有没有办法安全地全部替换,而不会覆盖以前的替代品?
我注意到,阿帕奇有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();
}
}