0

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.

When I run the above code, the "$" is displayed, any ideas?

4
  • 1
    You completely misunderstand the purpose of replaceAll. You should read documentation and discover, that replaceAll is the method for replacing the regular expression, not just the exact matching string. Here replace should be used. Commented Apr 13, 2015 at 9:16
  • In additon to what Dmitry Ginzburg said. In regular expressions, $ has special meaning which is end of line. Means, You are replacing end of line character with blank. Commented Apr 13, 2015 at 9:18
  • Apologies, your answer is correct and I'm using it in my code now, that's the method I was looking for. I have not read the documentation, that's my fault. Commented Apr 13, 2015 at 9:22
  • Ashwani, I'm making a "who wants to be a millionare" game, that's why I have an array of four Strings, one has a $ sign indicating the correct answer. I wanted to remove it before displaying it, but before doing so, record the correct answer to compare with user input later on. What other options do I have? Should I use a different char? or is there a complete different and more efficient method? Commented Apr 13, 2015 at 9:27

6 Answers 6

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:

  1. Keep using replaceAll(); then you have to "escape the special character"; by using replaceAll("\\$", ""), or "[$]" as others have pointed out.
  2. Use a different method, like replace() that doesn't expect a "regular expression pattern string".
Sign up to request clarification or add additional context in comments.

2 Comments

For 1 could also do replaceAll ("[$]", "");
As I've already pointed out in the other answer, replace(char, char) is also suitable: docs.oracle.com/javase/7/docs/api/java/lang/…, but in this case you can't use, because there's no "empty" symbol.
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.

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

Comments

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.

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

Comments

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.

You can also use replace() which take string

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

6 Comments

And the better solution is just replacing replaceAll with replace, no need to escape then.
Your answer solves the issue at hand; but doesn't explain at all why this will work.
I used replace instead of replaceAll and it worked, thanks Dmitry, and singhakash's answer is correct also, thanks for the explanation, I didn't know I had to identify special characters differently. Why does special char works without // in replace method, but not replaceAll?
@DmitryGinzburg But if the person doesn't pay attention, then he might turn to "replace(char, char)" - which only replaces once; not "all". So be careful when talking about "better" solutions.
|
1

Just use possibleAnswers[y].replace("$", ""); to remove "$" from string.

7 Comments

As said before: your answer solves the issue at hand; but doesn't explain at all why this will work.
public String replace(CharSequence paramCharSequence1, CharSequence paramCharSequence2) { return Pattern .compile(paramCharSequence1.toString(), 16) .matcher(this) .replaceAll( Matcher.quoteReplacement(paramCharSequence2.toString())); }
Edit your answer if you want to improve it. And btw: just adding the code you put in your comment will not improve the quality of the answer. The point is: your answer is missing an explanation. More code does explain nothing.
As per my knowledge replaceAll acceps only regex string which will take more time to compile target string compared to replace() function..
Some more explaination : Pattern.compile() function in both function take different time to compile as replace() function gives input to compile() function as CharSequence and replaceAll() function gives input to compile() function as regex string which will take more time to compile.
|
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.

Thanks for all the help guys, truly appreciated.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.