2

I am trying to replace characters in a string

for (String word : stringArray){
    int k = 33;
    char d ="";
    while (k < 64){
        char c = k;
        word = word.replace(c, ""); // THIS LINE GIVES AN ERROR
        k++;
    }
}

I am trying to get rid of all the non-letter characters between ASCII id 32 (!) and ASCII id 64 (@), inclusive. I am using stringid.replace(char, char) as suggested here.

However, "" in the second argument of replace is being treated as a string in Eclipse, which is giving an error as "type mismatch" (since replace expects arguments which are both characters).

I have tried '' instead of "" but that doesn't seem to fix the problem. What should I do? Thanks in advance.

EDIT: changed word.replace(c, ""); to word = word.replace(c,""); . Thanks to commenter for pointing this out. However, the problem still occurs.

Community
  • 1
  • 1
Zubin Mukerjee
  • 166
  • 2
  • 11
  • 2
    You forgot to read the very first sentence of the answer you linked to: **Strings in Java are immutable.** word.replace(c, "") does not change the String, because Strings are immutable and can’t be changed. – VGR May 10 '17 at 03:33
  • Thank you. I will edit the line in question to this `word = word.replace(c, "");` ... however, the problem I am asking about still occurs :( – Zubin Mukerjee May 10 '17 at 03:35
  • Possible duplicate of [How to represent empty char in Java Character class](http://stackoverflow.com/questions/8534178/how-to-represent-empty-char-in-java-character-class) – Andrew Li May 10 '17 at 03:38
  • 1
    ^ Exact duplicate, and make sure to *read the second answer* which is the correct one. – Andrew Li May 10 '17 at 03:38
  • @AndrewLi Thank you! So essentially, for what I am trying to do, str replace is not what I need ... could you possibly suggest an alternative? – Zubin Mukerjee May 10 '17 at 03:40
  • @AndrewLi The way I understand it now, str replace with two character arguments cannot do what I want, but str replace with two string arguments can do it, like a few of the answers show below ... this is not very intuitive, but I am sure there is reason behind how this is designed – Zubin Mukerjee May 10 '17 at 04:15

3 Answers3

2

You have to change quite a bit of your code for this to work:

for (int i = 0; i < stringArray.length; i++) {
    int k = 33;
    while (k < 64){
        stringArray[i] = stringArray[i].replace("" + k, "");
        k++;
    }

}

The key points are:

  • Strings are immutable, so if you modify them, you have to reassign them
  • If you modify an iteration variable in a for loop, the original value in the array won't be changed, you also have to reassign it inside the array
  • There's no "empty char", so the replacement must be between strings
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    Yes, this is the correct answer - `for (String word : stringArray)` assigns a copy of each element to `word`, not a reference. – Ken Y-N May 10 '17 at 03:42
  • @ÓscarLópez Thank you, +1 ! – Zubin Mukerjee May 10 '17 at 03:43
  • Hmm, this is not working as it turns out, I think it needs to be `replaceAll` ? – Zubin Mukerjee May 11 '17 at 02:43
  • @ZubinMukerjee `replace()` already substitutes _all_ occurrences of the given string (check the [docs](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-)), `replaceAll()` is different, as it takes a regular expression as parameter. Maybe the chars that need to be replaced are not in the range that you assumed? I can't know, I have no idea what's your input. – Óscar López May 11 '17 at 03:15
1

Why don't you put all the characters that you want to replace in a separate string and use regex to replace them all from your string?

String charsToBeReplaced = "[!\"#$%&'()*+,-.\\/0123456789:;<=>?@]"; //your ASCII 33 to 64 characters... notice that forward slash and double quotes is escaped here

for (int i=0; i<stringArray.length; i++){
    stringArray[i] = stringArray[i].replaceAll(charsToBeReplaced, "");
}

DEMO

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
1

Just to present another answer using regular expressions:

Pattern p = Pattern.compile("[\\x21-\\x40]");
for (int i=0; i<stringArray.length; i++){
    stringArray[i] = p.matcher(stringArray[i]).replaceAll("");
}

This is probably more efficient that Raman Sahasi's answer as we only have to compile the pattern once.

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • I trust that this works but right now it looks like magic to me - I will have to read up about `Pattern` ... regardless, thanks for the answer! +1 – Zubin Mukerjee May 10 '17 at 04:12