替换字符串中的多个子字符串
replace multiple sub-strings in a string
此函数用于将字符串中的某些子字符串替换为各自的值。
// 映射(string_to_replace, string_to_replace_with)
String template = "ola ala kala pala sala";
StringBuilder populatedTemplate = new StringBuilder();
HashMap<String, String> map = new HashMap<>();
map.put("ola", "patola");
map.put("pala", "papala");
int i=0;
for (String word : template.split("'")) {
populatedTemplate.append( map.getOrDefault(word, word));
populatedTemplate.append(" ");
}
System.out.println(populatedTemplate.toString());
如果要替换的子字符串被“ ”包围,则上述函数可以正常工作(space)。
Ex- 字符串 => "Hey {how} are $=you"
如果要替换的子字符串是 "Hey" 或 "you",则它可以正常工作。问题是我想替换 "how" 和 "you"。
如何在不增加复杂性的情况下实现这一点?
如果你只想替换你在地图中的单词,其余的保持原样,你可以继续下一步:
String template = "Hey {how} are $=you";
StringBuilder populatedTemplate = new StringBuilder();
Map<String, String> map = new HashMap<>();
map.put("how", "HH");
map.put("you", "YY");
// Pattern allowing to extract only the words
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(template);
int fromIndex = 0;
while (matcher.find(fromIndex)) {
// The start index of the current word
int startIdx = matcher.start();
if (fromIndex < startIdx) {
// Add what we have between two words
populatedTemplate.append(template, fromIndex, startIdx);
}
// The current word
String word = matcher.group();
// Replace the word by itself or what we have in the map
populatedTemplate.append(map.getOrDefault(word, word));
// Start the next find from the end index of the current word
fromIndex = matcher.end();
}
if (fromIndex < template.length()) {
// Add the remaining sub String
populatedTemplate.append(template, fromIndex, template.length());
}
System.out.println(populatedTemplate);
输出:
Hey {HH} are $=YY
响应更新:
假设您不仅要替换单词,还要替换任何类似 ${questionNumber}
的内容,您将需要像这样动态创建正则表达式:
String template = "Hey {how} are $=you id=minScaleBox-${questionNumber}";
...
map.put("${questionNumber}", "foo");
StringBuilder regex = new StringBuilder();
boolean first = true;
for (String word : map.keySet()) {
if (first) {
first = false;
} else {
regex.append('|');
}
regex.append(Pattern.quote(word));
}
Pattern pattern = Pattern.compile(regex.toString());
...
输出:
Hey {HH} are $=YY id=minScaleBox-foo
此函数用于将字符串中的某些子字符串替换为各自的值。
// 映射(string_to_replace, string_to_replace_with)
String template = "ola ala kala pala sala";
StringBuilder populatedTemplate = new StringBuilder();
HashMap<String, String> map = new HashMap<>();
map.put("ola", "patola");
map.put("pala", "papala");
int i=0;
for (String word : template.split("'")) {
populatedTemplate.append( map.getOrDefault(word, word));
populatedTemplate.append(" ");
}
System.out.println(populatedTemplate.toString());
如果要替换的子字符串被“ ”包围,则上述函数可以正常工作(space)。
Ex- 字符串 => "Hey {how} are $=you" 如果要替换的子字符串是 "Hey" 或 "you",则它可以正常工作。问题是我想替换 "how" 和 "you"。
如何在不增加复杂性的情况下实现这一点?
如果你只想替换你在地图中的单词,其余的保持原样,你可以继续下一步:
String template = "Hey {how} are $=you";
StringBuilder populatedTemplate = new StringBuilder();
Map<String, String> map = new HashMap<>();
map.put("how", "HH");
map.put("you", "YY");
// Pattern allowing to extract only the words
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(template);
int fromIndex = 0;
while (matcher.find(fromIndex)) {
// The start index of the current word
int startIdx = matcher.start();
if (fromIndex < startIdx) {
// Add what we have between two words
populatedTemplate.append(template, fromIndex, startIdx);
}
// The current word
String word = matcher.group();
// Replace the word by itself or what we have in the map
populatedTemplate.append(map.getOrDefault(word, word));
// Start the next find from the end index of the current word
fromIndex = matcher.end();
}
if (fromIndex < template.length()) {
// Add the remaining sub String
populatedTemplate.append(template, fromIndex, template.length());
}
System.out.println(populatedTemplate);
输出:
Hey {HH} are $=YY
响应更新:
假设您不仅要替换单词,还要替换任何类似 ${questionNumber}
的内容,您将需要像这样动态创建正则表达式:
String template = "Hey {how} are $=you id=minScaleBox-${questionNumber}";
...
map.put("${questionNumber}", "foo");
StringBuilder regex = new StringBuilder();
boolean first = true;
for (String word : map.keySet()) {
if (first) {
first = false;
} else {
regex.append('|');
}
regex.append(Pattern.quote(word));
}
Pattern pattern = Pattern.compile(regex.toString());
...
输出:
Hey {HH} are $=YY id=minScaleBox-foo