1

I am trying replace all white space of below string as blank

89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49

Desire output should be -

89504e470d0a1a0a0000000d49

I have tried with below code. But below code is not working.

public static void main(String[] args) {
        String input = "89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49"; 
        System.out.println(input);
        input.replaceAll(" ", "").replaceAll("[\\s\\u00A0]+$", "");
        input.replaceAll("\\w", "")
        System.out.println(input);
    }
Avijit
  • 1,770
  • 5
  • 16
  • 34
  • @Tunaki the problem is string immutability, but the reference to the question is incorrect. The problem exposed in the question you reference is totally different – Davide Lorenzo MARINO Jun 08 '16 at 08:08
  • @DavideLorenzoMARINO On the contrary, it goes deeper in the issue, which explains it a lot better. There is always this one... http://stackoverflow.com/questions/17553159/cant-delete-words-from-a-string-with-replaceall – Tunaki Jun 08 '16 at 08:10
  • You can use input = input.replaceAll("\\s+","") to fix this – Don Lee Jun 08 '16 at 08:16
  • @Tunaki the second question seems closer to what is asked here – Davide Lorenzo MARINO Jun 08 '16 at 09:55

2 Answers2

1
input = input.replaceAll(" ", "");

should do it.

0

String are not modifiable.

The method replaceAll doesn't change the original string, but creates a new one.

So you need to reassign the result of replaceAll to input

input = input.replaceAll(" ", "");
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56