Java 替换字符串中的多个子字符串,数组vs HashMap
下面定义了两个函数。它们执行完全相同的功能,即输入一个模板(其中需要替换一些子字符串)和字符串值数组(要替换的键值对,例如:[substrotreplace1,value1,substrotreplace1,value2,…]),并返回替换的字符串 在第二个函数中,我迭代模板中的单词,并搜索hashmap中存在的相关关键字,然后搜索下一个单词。若我想用一些子字符串替换一个单词,而我又想用一些其他键值替换这个子字符串,那个么我需要在模板上迭代两次。我就是这么做的 我想知道我应该使用哪一个,为什么?任何比这些更好的选择都是受欢迎的 第一功能Java 替换字符串中的多个子字符串,数组vs HashMap,java,arrays,hashmap,Java,Arrays,Hashmap,下面定义了两个函数。它们执行完全相同的功能,即输入一个模板(其中需要替换一些子字符串)和字符串值数组(要替换的键值对,例如:[substrotreplace1,value1,substrotreplace1,value2,…]),并返回替换的字符串 在第二个函数中,我迭代模板中的单词,并搜索hashmap中存在的相关关键字,然后搜索下一个单词。若我想用一些子字符串替换一个单词,而我又想用一些其他键值替换这个子字符串,那个么我需要在模板上迭代两次。我就是这么做的 我想知道我应该使用哪一个,为什么?
public static String populateTemplate1(String template, String... values) {
String populatedTemplate = template;
for (int i = 0; i < values.length; i += 2) {
populatedTemplate = populatedTemplate.replace(values[i], values[i + 1]);
}
return populatedTemplate;
}
公共静态字符串populateTemplate1(字符串模板、字符串…值){
字符串populatedTemplate=模板;
对于(int i=0;i
第二功能
public static String populateTemplate2(String template, String... values) {
HashMap<String, String> map = new HashMap<>();
for (int i = 0; i < values.length; i += 2) {
map.put(values[i],values[i+1]);
}
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());
int N0OfIterationOverTemplate =2;
// Pattern allowing to extract only the words
// Pattern pattern = Pattern.compile("\\w+");
StringBuilder populatedTemplate=new StringBuilder();;
String temp_template=template;
while(N0OfIterationOverTemplate!=0){
populatedTemplate = new StringBuilder();
Matcher matcher = pattern.matcher(temp_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(temp_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));
if (map.get(word) == null) {
populatedTemplate.append(word);
}
else {
populatedTemplate.append(map.get(word));
}
// Start the next find from the end index of the current word
fromIndex = matcher.end();
}
if (fromIndex < temp_template.length()) {
// Add the remaining sub String
populatedTemplate.append(temp_template, fromIndex, temp_template.length());
}
N0OfIterationOverTemplate--;
temp_template=populatedTemplate.toString();
}
return populatedTemplate.toString();
}
public静态字符串populateTemplate2(字符串模板、字符串…值){
HashMap=newHashMap();
对于(int i=0;i
至少出于两个原因,确定第一个:
第一个函数更清晰、更容易理解。我更喜欢它,除非你(通过剖析器)发现它花费了相当多的时间并减慢了你的应用程序。然后你就可以找出如何优化它。既然你可以把事情简单化,为什么还要把事情复杂化呢 请记住,简单的解决方案往往是最好的 仅供参考,如果元素数为奇数,您将获得ArrayIndexOutOfBoundsException 我建议作出以下改善:
public static String populateTemplate(String template, String... values) {
String populatedTemplate = template;
int nextTarget = 2;
int lastTarget = values.length - nextTarget;
for (int i = 0; i <= lastTarget; i += nextTarget) {
String target = values[i];
String replacement = values[i + 1];
populatedTemplate = populatedTemplate.replace(target, replacement);
}
return populatedTemplate;
}
公共静态字符串populateTemplate(字符串模板、字符串…值){
字符串populatedTemplate=模板;
int nextTarget=2;
int lastTarget=values.length-nextTarget;
对于(int i=0;i