gpt4 book ai didi

java - 如何在java中替换包含字符@和#的确切字符串

转载 作者:行者123 更新时间:2023-12-01 14:22:14 26 4
gpt4 key购买 nike

您好,我在替换包含 # 和 @ 字符的确切字符串时遇到了麻烦。我试过mytext.replaceAll('\\b('+text+')\\b',"testtest");但也没有帮助。精确的字符串意味着精确的单词。

String myStr = "test1 test";
myStr= myStr.replaceAll('\\b('+ Pattern.quote("test") +')\\b',"notest")//out put string "test1 notest"
//Exact word means only "test" is getting replace not "test" from "test1" word to notest1
String myStr1 = "test1 #test";
myStr1 = myStr1.replaceAll('\\b('+Pattern.quote("#test") +')\\b',"notest")//out put string "test1 #test", #test is not replaced

有人可以帮助我吗?提前致谢

最佳答案

您有几个编译错误。

这段代码对我有用:

String myStr = "test1 test";
myStr= myStr.replaceAll("\\b("+ Pattern.quote("test") +")\\b","notest");
System.out.println(myStr);
String myStr1 = "test1 #test";
myStr1 = myStr1.replaceAll("\\b("+Pattern.quote("#test") +")\\b","notest");
System.out.println(myStr);

关于java - 如何在java中替换包含字符@和#的确切字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com