Java String.replace/replaceAll 不起作用 [英] Java String.replace/replaceAll not working

查看:40
本文介绍了Java String.replace/replaceAll 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图在 Java 中解析包含(左)方括号的字符串输入.我有 str.replace("\[", ""),但这绝对没有任何作用.我也试过 replaceAll ,使用不止一种不同的正则表达式,但输出始终不变.我的一部分想知道这是否可能是因为我所有的反斜杠字符都显示为日元符号(自从我将日语添加到我的语言中),但这种情况已经一年多了,并没有给我造成任何影响以前有类似的问题.

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?

推荐答案

字符串在 Java 中是不可变的.确保将返回值重新分配给相同的 String 变量:

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

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

对于普通的replace方法,不需要转义括号:

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

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

这篇关于Java String.replace/replaceAll 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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