java中replaceall无法去除,Java String.replace / replaceAll无法正常工作

"在Java中遇到字符串解析问题,尝试使用replace和replaceAll方法去除方括号,但未生效。问题可能源于字符串的不可变性或正则表达式的使用。解决方案是确保重新赋值返回的结果,如:str = str.replaceAll("\[", ""); 对于replace方法,无需转义,可以使用str = str.replace("[", "");"
摘要由CSDN通过智能技术生成

So, I'm trying to parse a String input in Java that contains (opening) square brackets. I have str.replace("\\[", ""), but this does absolutely nothing. I've tried replaceAll also, with more than one different regex, but the output is always unchanged. Part of me wonders if this is possibly caused by the fact that all my back-slash characters appear as yen symbols (ever since I added Japanese to my languages), but it's been that way for over a year and hasn't caused me any issues like this before.

Any idea what I might be doing wrong here?

解决方案

Strings are immutable in Java. Make sure you re-assign the return value to the same String variable:

str = str.replaceAll("\\[", "");

For the normal replace method, you don't need to escape the bracket:

str = str.replace("[", "");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值