无法替换所有美元符号 [英] Not able to replace all for dollar sign

查看:107
本文介绍了无法替换所有美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁能告诉我为什么运行此方法用$符号替换值时为什么我会遇到bouns异常之外的索引?

can anyone advise why i encountered index out of bouns exception when running this method to replace the value by $ sign?

例如我传递消息$$vmdomodm$$

message = message.replaceAll("$", "$");

我试图查看此论坛主题,但无法理解内容

I tried to look at this forum thread but couldnt understand the content

http://www.coderanch.com/t/383666/java/java/String-replaceAll

推荐答案

这是您需要使用转义字符的特殊字符

It is special character you need to use escape character

尝试使用此\\$

并且您尝试用相同的内容替换内容在您的代码中没有意义

and it doesn't make sense in your code you are trying to replacing the content with same

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 $$

这篇关于无法替换所有美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆