如何用java中相应的字符替换特定字符?

时间:2017-09-09 17:47:37

标签: java string replace

我想这样做:将所有 Traceback (most recent call last): File "C:/Users/ankita.a.rath/Desktop/my_codes/Rasa_nlu/rasa_nlu-master/train_spacy_ner.py", line 248, in <module> main("en", "new_model") File "C:/Users/ankita.a.rath/Desktop/my_codes/Rasa_nlu/rasa_nlu-master/train_spacy_ner.py", line 238, in main print (doc2.vector) File "spacy/tokens/doc.pyx", line 275, in spacy.tokens.doc.Doc.vector.__get__ (spacy/tokens/doc.cpp:7291) self._vector = sum(t.vector for t in self) / len(self) File "spacy/tokens/doc.pyx", line 275, in genexpr (spacy/tokens/doc.cpp:7114) self._vector = sum(t.vector for t in self) / len(self) File "spacy/tokens/token.pyx", line 240, in spacy.tokens.token.Token.vector.__get__ (spacy/tokens/token.cpp:7249) raise ValueError( ValueError: Word vectors set to length 0. This may be because you don't have a model installed or loaded, or because your model doesn't include word vectors. For more info, see the documentation: https://spacy.io/docs/usage 替换为ck,将所有k替换为dd,将所有wr替换为f还有10个像这样的替代品。 我可以用m等来做,但它接缝很傻,而且很慢。在java中是否有任何类似的功能? 例如replace("ck","k").replace("dd","wr")

2 个答案:

答案 0 :(得分:2)

使用appendReplacement循环。

这是一种通用的方法:

private static String replace(String input, Map<String, String> mappings) {
    StringBuffer buf = new StringBuffer();
    Matcher m = Pattern.compile(toRegex(mappings.keySet())).matcher(input);
    while (m.find())
        m.appendReplacement(buf, Matcher.quoteReplacement(mappings.get(m.group())));
    return m.appendTail(buf).toString();
}
private static String toRegex(Collection<String> keys) {
    return keys.stream().map(Pattern::quote).collect(Collectors.joining("|"));
}

如果您不使用Java 8+,第二种方法是:

private static String toRegex(Collection<String> keys) {
    StringBuilder regex = new StringBuilder();
    for (String key : keys) {
        if (regex.length() != 0)
            regex.append("|");
        regex.append(Pattern.quote(key));
    }
    return regex.toString();
}

测试代码

Map<String, String> mappings = new HashMap<>();
mappings.put("ck","k");
mappings.put("dd","wr");
mappings.put("f", "m");
System.out.println(replace("odd flock", mappings)); // prints: owr mlok

有关正在运行的版本,请参阅IDEONE

答案 1 :(得分:1)

Map<String, String> replacementMap = new HashMap<String, String>();
replacementMap.put("ck", "k");
replacementMap.put("dd", "wr");
replacementMap.put("f", "m");
// ...

String resultStr = "Abck fdddk wr fmck"; // whatever string to process
StringBuilder builder = new StringBuilder(resultStr); // wrap it in builder

Iterator<String> iterator = replacementMap.keySet().iterator();
while (iterator.hasNext()) {
    String strToReplace = iterator.next();
    replaceAll(builder, strToReplace, replacementMap.get(strToReplace));
}
System.out.println("Result is: " + builder.toString());

public static void replaceAll(StringBuilder builder, String from, String to) {
    int index = builder.indexOf(from);
    while (index != -1) {
        builder.replace(index, index + from.length(), to);
        index += to.length(); // Move to the end of the replacement
        index = builder.indexOf(from, index);
    }
}

replaceAll()方法是从这个Jon Skeet's answer

借来的

替换replaceAll()int他的例子是使用apache commons库,有StrBuilder类提供replaceAll()方法。见this answer