Java String.replace / replaceAll无法正常工作 [英] Java String.replace/replaceAll not working
问题描述
所以,我正在尝试用Java解析包含(打开)方括号的String输入。我有 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("\\[", "");
对于正常的替换
方法,你不要不需要逃避括号:
For the normal replace
method, you don't need to escape the bracket:
str = str.replace("[", "");
这篇关于Java String.replace / replaceAll无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!