I've been trying to remove a specific character from an Array of Strings but continue to fail. The char is "$", I don't know what I'm doing wrong so hoping someone would be able to point to the right direction, my code:
我一直在尝试从字符串数组中删除特定字符,但仍然会失败。 char是“$”,我不知道我做错了什么,所以希望有人能够指出正确的方向,我的代码:
for (int y = 0; y<possibleAnswers.length;y++) {
display = possibleAnswers[y].replaceAll("$", "");
System.out.println(display);
}
possibleAnswers contains 4 Strings, one of the 4 has a "$", I want to remove it before displaying it.
possibleAnswers包含4个字符串,其中一个有“$”,我想在显示它之前删除它。
When I run the above code, the "$" is displayed, any ideas?
当我运行上面的代码时,显示“$”,任何想法?
6 个解决方案
#1
3
The problem with your code is that replaceAll()
expects a "regular expression". The $ character has a specific meaning when used in a regular expression. Therefore you have two options:
你的代码的问题是replaceAll()需要一个“正则表达式”。在正则表达式中使用时,$字符具有特定含义。因此,您有两种选择:
- Keep using
replaceAll()
; then you have to "escape the special character"; by usingreplaceAll("\\$", "")
, or "[$]" as others have pointed out. - 继续使用replaceAll();那么你必须“逃避特殊性格”;通过使用replaceAll(“\\ $”,“”)或“[$]”,正如其他人指出的那样。
- Use a different method, like
replace()
that doesn't expect a "regular expression pattern string". - 使用不同的方法,例如不期望“正则表达式模式字符串”的replace()。
#2
2
replaceAll()
accepts a regex, not just a character. When you say "$", you're not telling it to match the '$' character, but to match the ending position of the String or before a newline before the end of the String.
replaceAll()接受正则表达式,而不仅仅是一个字符。当你说“$”时,你并没有告诉它匹配'$'字符,而是匹配字符串的结束位置或字符串结束前的换行符之前。
You need to escape the '$', so it knows to match just the character, and not treat it like it's special regex meaning.
你需要逃避'$',所以它知道只匹配角色,而不是像它的特殊正则表达式那样对待它。
Do this like: possibleAnswers[y].replaceAll("\\$", "");
这样做:possibleAnswers [y] .replaceAll(“\\ $”,“”);
#3
1
IN your code the $
is keyword
in regex
for matching end of line
i.e. $. SO you will have to escape
it as below.
在你的代码中,$是regex中的关键字,用于匹配行尾,即$。所以你必须如下所述逃脱它。
display = possibleAnswers[y].replaceAll("\\$", "");
#4
1
Try
尝试
possibleAnswers[y].replaceAll("\\$", "");
escape the character because $
is a special character in regular expression and since replaceAll()
take regular expression the string you passed is unidentified.
转义字符是因为$是正则表达式中的特殊字符,因为replaceAll()采用正则表达式,所以传递的字符串是未识别的。
You can also use replace()
which take string
你也可以使用带字符串的replace()
possibleAnswers[y].replace("$", "");
#5
1
Just use possibleAnswers[y].replace("$", ""); to remove "$" from string.
只需使用possibleAnswers [y] .replace(“$”,“”);从字符串中删除“$”。
#6
0
for (int y = 0; y < possibleAnswers.length; y++) {
display = possibleAnswers[y].replace("$", "");
System.out.println(display);
}
As suggested, I used replace method instead of replaceAll and it did the job. At the same time I learned to look at documentation for deeper understanding of such methods.
正如所建议的那样,我使用replace方法而不是replaceAll,它完成了这项工作。与此同时,我学会了阅读文档,以便更深入地理解这些方法。
Thanks for all the help guys, truly appreciated.
感谢所有帮助人员,真的很感激。
#1
3
The problem with your code is that replaceAll()
expects a "regular expression". The $ character has a specific meaning when used in a regular expression. Therefore you have two options:
你的代码的问题是replaceAll()需要一个“正则表达式”。在正则表达式中使用时,$字符具有特定含义。因此,您有两种选择:
- Keep using
replaceAll()
; then you have to "escape the special character"; by usingreplaceAll("\\$", "")
, or "[$]" as others have pointed out. - 继续使用replaceAll();那么你必须“逃避特殊性格”;通过使用replaceAll(“\\ $”,“”)或“[$]”,正如其他人指出的那样。
- Use a different method, like
replace()
that doesn't expect a "regular expression pattern string". - 使用不同的方法,例如不期望“正则表达式模式字符串”的replace()。
#2
2
replaceAll()
accepts a regex, not just a character. When you say "$", you're not telling it to match the '$' character, but to match the ending position of the String or before a newline before the end of the String.
replaceAll()接受正则表达式,而不仅仅是一个字符。当你说“$”时,你并没有告诉它匹配'$'字符,而是匹配字符串的结束位置或字符串结束前的换行符之前。
You need to escape the '$', so it knows to match just the character, and not treat it like it's special regex meaning.
你需要逃避'$',所以它知道只匹配角色,而不是像它的特殊正则表达式那样对待它。
Do this like: possibleAnswers[y].replaceAll("\\$", "");
这样做:possibleAnswers [y] .replaceAll(“\\ $”,“”);
#3
1
IN your code the $
is keyword
in regex
for matching end of line
i.e. $. SO you will have to escape
it as below.
在你的代码中,$是regex中的关键字,用于匹配行尾,即$。所以你必须如下所述逃脱它。
display = possibleAnswers[y].replaceAll("\\$", "");
#4
1
Try
尝试
possibleAnswers[y].replaceAll("\\$", "");
escape the character because $
is a special character in regular expression and since replaceAll()
take regular expression the string you passed is unidentified.
转义字符是因为$是正则表达式中的特殊字符,因为replaceAll()采用正则表达式,所以传递的字符串是未识别的。
You can also use replace()
which take string
你也可以使用带字符串的replace()
possibleAnswers[y].replace("$", "");
#5
1
Just use possibleAnswers[y].replace("$", ""); to remove "$" from string.
只需使用possibleAnswers [y] .replace(“$”,“”);从字符串中删除“$”。
#6
0
for (int y = 0; y < possibleAnswers.length; y++) {
display = possibleAnswers[y].replace("$", "");
System.out.println(display);
}
As suggested, I used replace method instead of replaceAll and it did the job. At the same time I learned to look at documentation for deeper understanding of such methods.
正如所建议的那样,我使用replace方法而不是replaceAll,它完成了这项工作。与此同时,我学会了阅读文档,以便更深入地理解这些方法。
Thanks for all the help guys, truly appreciated.
感谢所有帮助人员,真的很感激。