简体   繁体   English

从字符串数组中删除字符

[英]removing a char from an array of Strings

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: 字符是“ $”,我不知道我在做什么错,所以希望有人能够指出正确的方向,我的代码:

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个字符串,这4个字符串之一具有“ $”,我想在显示它之前将其删除。

When I run the above code, the "$" is displayed, any ideas? 当我运行上面的代码时,显示“ $”,有什么想法吗?

The problem with your code is that replaceAll() expects a "regular expression". 您的代码的问题在于replaceAll()需要一个“正则表达式”。 The $ character has a specific meaning when used in a regular expression. 在正则表达式中使用$字符具有特定含义。 Therefore you have two options: 因此,您有两个选择:

  1. Keep using replaceAll() ; 继续使用replaceAll() ; then you have to "escape the special character"; 然后您必须“转义特殊字符”; by using replaceAll("\\\\$", "") , or "[$]" as others have pointed out. 正如其他人指出的那样,可以使用replaceAll("\\\\$", "")或“ [$]”。
  2. Use a different method, like replace() that doesn't expect a "regular expression pattern string". 使用其他方法,例如replace() ,它不希望使用“正则表达式模式字符串”。

replaceAll() accepts a regex, not just a character. replaceAll()接受一个正则表达式,而不仅仅是一个字符。 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. 当您说“ $”时,并不是要告诉它匹配“ $”字符,而是要匹配字符串的结束位置或字符串末尾的换行符之前。

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("\\\\$", "");

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()字符串的replace()

 possibleAnswers[y].replace("$", "");

IN your code the $ is keyword in regex for matching end of line ie $. 在您的代码中, regex$ is keyword用于匹配end of line即$。 SO you will have to escape it as below. 因此,您将必须按如下所示进行escape

display = possibleAnswers[y].replaceAll("\\$", "");

Just use possibleAnswers[y].replace("$", ""); 只需使用可能的答案[y] .replace(“ $”,“”); to remove "$" from string. 从字符串中删除“ $”。

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. 如建议的那样,我使用了replace方法而不是replaceAll,它完成了工作。 At the same time I learned to look at documentation for deeper understanding of such methods. 同时,我学会了查看文档以更深入地了解此类方法。

Thanks for all the help guys, truly appreciated. 感谢所有帮助人员,非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM