我需要将以下字符串的所有[quote]替换为",将[/quote]替换为":
[quote]fgfhfgh [quote] vbbb[/quote]ghhhhjj[/quote] 结果应该是这样的:
"fghfdgfdh "gghj vbbb"ghhhhjj"我使用了这个,但不能这样做:
finalRecieveString = (String) uivdHashTable.get("pendingString");
String ReplaceQuote=finalRecieveString.replaceAll("[quote]", "\"");
ReplaceFinalQuoteString=ReplaceQuote.replaceAll("[/quote]", "\"");发布于 2012-08-29 19:51:23
您必须转义方括号,因为replaceAll()期望使用正则表达式,而方括号是其语法的一部分。所以试试吧:
finalRecieveString = (String) uivdHashTable.get("pendingString");
String ReplaceQuote=finalRecieveString.replaceAll("\[quote\]", "\"");
ReplaceFinalQuoteString=ReplaceQuote.replaceAll("\[/quote\]", "\"");您还可以链接替换:
ReplaceFinalQuoteString = finalRecieveString.replaceAll("\[quote\]", "\"").replaceAll("\[/quote\]", "\"");PS:在"=“前后添加空格可以提高代码的可读性。
发布于 2012-08-29 19:52:08
String string = "quotemucho grando wrote: quotemucho grando wrote: vbbb/quoteghhhhjj/quote";
String tmp_str = string.replace("[quote]","\"");
String str_ans = tmp_str.replace("[/quote]","\"");
你的ans str_ans = "mucho grando wrote:"mucho wrote: vbbb"ghhhhjj“
https://stackoverflow.com/questions/12176997
复制相似问题