无法替换所有美元符号
任何人都可以建议为什么我在运行此方法时使用$
sign替换值时遇到bounsexception索引?
例如,我传递了一条消息$$vmdomodm$$
message = message.replaceAll("$", "$");
我试着看看这个论坛post,但无法理解内容
http://www.coderanch.com/t/383666/java/java/String-replaceAll
这是你需要使用转义字符的特殊字符
试试这个\\$
在你的代码中,你试图用相同的内容替换内容是没有意义的
String message = "$$hello world $$"; message = message.replaceAll("\\$", "_"); System.out.println(message);
产量
__hello world __
更新
String message = "$hello world $$"; message = message.replaceAll("$", "\\$"); System.out.println(message);
产量
$hello world $$
既然你没有真正使用任何正则表达式而不是replaceAll你应该使用String#replace方法,如下所示:
message = message.replace("$", "$");