简体   繁体   English

如何在一行中替换多个字符串?

[英]How to replace more than a string in one line?

I want to insert "OB" before every vowel. 我想在每个元音之前插入“OB”。 I have tried the code below: 我试过下面的代码:

String out=txt.toUpperCase();

out=out.replaceAll("A","OBA");
out=out.replaceAll("E","OBE");
out=out.replaceAll("I","OBI");
out=out.replaceAll("O","OBO");
out=out.replaceAll("U","OBU");
out=out.replaceAll("Y","OBY");

When I use that code above, it replaces A with OBA , but then when it comes to replacing O to OBO it replaces the O from the original text and also the O in OBA . 当我使用上面的代码,它取代AOBA ,但是当它涉及到更换OOBO它取代了O从原来的文本,也是OOBA For example, for "I WON'T" I want the output "OBI WOBON'T" , but instead it gives "OBOBI WOBON'T" as the O from OBI from the first line has been treated as a vowel. 例如,对于"I WON'T"我想要输出"OBI WOBON'T" ,而是它给出了"OBOBI WOBON'T"因为来自第一行的OBIO被视为元音。

I need a solution which doesn't replace the new O from the encryption. 我需要一个解决方案,它不会取代加密中的新O

Since replaceAll takes regex, you can use references to captured elements in your replacement string: 由于replaceAll采用正则表达式,因此您可以在替换字符串中使用对捕获元素的引用:

out=out.replaceAll("[AEIOUY]", "OB$0");
  • [AEIOUY] captures a single character from the AEIOUY list [AEIOUY]AEIOUY列表中捕获单个字符
  • $0 in the replacement string stands for the character that has been captured. 替换字符串中的$0代表已捕获的字符。

Here is a demo . 这是一个演示

您可以使用$1来引用匹配的组,因此替换为AB$1

out.replaceAll("([AEIOUY])", "OB$1")

move "O","OBO" to the start will prevent duplication 移动“O”,“OBO”开始将防止重复

String out=txt.toUpperCase();

out=out.replaceAll("O","OBO");
out=out.replaceAll("A","OBA");
out=out.replaceAll("E","OBE");
out=out.replaceAll("I","OBI");
out=out.replaceAll("U","OBU");
out=out.replaceAll("Y","OBY");

If you don't want to use regexes: 如果您不想使用正则表达式:

HashMap<Character, String> replace = new HashMap<Character, String>() {{
            put('A',"OBA");
            put('E',"OBE");
            put('I',"OBI");
            put('O',"OBO");
            put('U',"OBU");
            put('Y',"OBY");
}};

final StringBuilder res = new StringBuilder();
String test = "I WON'T";

test
 .chars()
 .mapToObj(c -> (char) c)
 .forEach(c -> res.append(replace.getOrDefault(c, Character.toString(c))));
System.out.println(res);

Something like this can also be done, replace OB with '-@' (any 2 characters that we can be sure of would not appear in the string) and in the end replace them with OB: 也可以这样做,用' - '替换OB(我们可以肯定的任何2个字符都不会出现在字符串中),最后用OB替换它们:

public static void main(String[] args) { String text = "I WON'T"; public static void main(String [] args){String text =“我不会”;

text = text.replaceAll("A", "-@A");
text = text.replaceAll("E", "-@E");
text = text.replaceAll("I", "-@I");
text = text.replaceAll("O", "-@O");
text = text.replaceAll("U", "-@U");
text = text.replaceAll("Y", "-@Y");
System.out.println(text);       
text = text.replaceAll("-@", "OB");     
System.out.println(text);
}

output 产量

-@I W-@ON'T OBI WOBON'T - @我W- @ ON'T OBI WOBON'T

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM