简体   繁体   English

以更有效的方式替换字符串中的一组子字符串?

[英]Replace a set of substring in a string in more efficient way?

I've to replace a set of substrings in a String with another substrings for example 我要用String中的一组子串替换另一个子串,例如

  1. "^t" with "\\t" "^t""\\t"
  2. "^=" with "\—" "^=""\—"
  3. "^+" with "\–" "^+""\–"
  4. "^s" with "\ " 带有"\ " "^s" "\ "
  5. "^?" with "." "."
  6. "^#" with "\\\\d" "^#""\\\\d"
  7. "^$" with "[a-zA-Z]" "[a-zA-Z]" "^$" "[a-zA-Z]"

So, I've tried with: 所以,我试过:

String oppip = "pippo^t^# p^+alt^shefhjkhfjkdgfkagfafdjgbcnbch^";

Map<String,String> tokens = new HashMap<String,String>();
tokens.put("^t", "\t");
tokens.put("^=", "\u2014");
tokens.put("^+", "\u2013");
tokens.put("^s", "\u00A0");
tokens.put("^?", ".");
tokens.put("^#", "\\d");
tokens.put("^$", "[a-zA-Z]");

String regexp = "^t|^=|^+|^s|^?|^#|^$";

StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(oppip);
while (m.find())
    m.appendReplacement(sb, tokens.get(m.group()));
m.appendTail(sb);
System.out.println(sb.toString()); 

But it doesn't work. 但它不起作用。 tokens.get(m.group()) throws an exception. tokens.get(m.group())抛出异常。

Any idea why? 知道为什么吗?

You don't have to use a HashMap . 您不必使用HashMap Consider using simple arrays, and a loop : 考虑使用简单数组和循环

String oppip = "pippo^t^# p^+alt^shefhjkhfjkdgfkagfafdjgbcnbch^";

String[] searchFor =
{"^t", "^=", "^+", "^s", "^?", "^#", "^$"},
         replacement =
{"\\t", "\\u2014", "\\u2013", "\\u00A0", ".", "\\d", "[a-zA-Z]"};

for (int i = 0; i < searchFor.length; i++)
    oppip = oppip.replace(searchFor[i], replacement[i]);

// Print the result.
System.out.println(oppip);

Here is an online code demo . 这是一个在线代码演示


For the completeness, you can use a two-dimensional array for a similar approach: 为了完整性,您可以使用二维数组来实现类似的方法:

String oppip = "pippo^t^# p^+alt^shefhjkhfjkdgfkagfafdjgbcnbch^";

String[][] tasks =
{
    {"^t", "\\t"},
    {"^=", "\\u2014"}, 
    {"^+", "\\u2013"}, 
    {"^s", "\\u00A0"}, 
    {"^?", "."}, 
    {"^#", "\\d"}, 
    {"^$", "[a-zA-Z]"}
};

for (String[] replacement : tasks)
    oppip = oppip.replace(replacement[0], replacement[1]);

// Print the result.
System.out.println(oppip);

In regex the ^ means "begin-of-text" (or "not" within a character class as negation). 在正则表达式中, ^表示“文本开头”(或者在字符类中“不”作为否定)。 You have to place a backslash before it, which becomes two backslashes in a java String. 你必须在它之前放一个反斜杠,它在java String中变成两个反斜杠。

String regexp = "\\^[t=+s?#$]";

I have reduced it a bit further. 我进一步减少了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 替换字符串中出现的所有子字符串 - 这在Java中更有效吗? - Replace all occurrences of substring in a string - which is more efficient in Java? 搜索String数组以查找子字符串的最有效方法 - Most efficient way to search String array for substring 更有效的拆分字符串的方法 - more efficient way to split string 是否有一种有效的方法来检测字符串是否包含一大组特征字符串中的 substring? - Is there an efficient way to detect if a string contains a substring which is in a large set of characteristic strings? 将 ArrayLists 用于一组类的更有效方法? - A more efficient way to using ArrayLists for a set of classes? 内存有效的方式来替换Java中的字符串 - Memory efficient way to replace string in Java 替换字符串中字符的有效方法(java)? - Efficient way to replace chars in a string (java)? 有没有更有效的方法来计算带有树状图的字符串的实例? - Is there a more efficient way of counting instances of a string with treemaps? 有没有更有效的方法来检查字符串中的字符? - Is there a more efficient way to check characters in a string? 替换字符串中多个字符的有效方法是什么? - What is an efficient way to replace many characters in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM