3

So I'm an IB student and I'm taking Comp. Sci. we need to create a code to solve a problem, as i was writing my Java code I found the String.replaceAll(); that is helpful in writing my code. I had to remove any white spaces, and any new lines, so I found \\s+ and \\n. So as I enter the code String.replaceAll("\\s+",""); it didn't work, same with String.replaceAll("\\n","");. I have tried removing the " " but BlueJ, sends an error saying that \ is an illegal character. I checked if maybe the String.replaceAll(); doesn't works but it does work, so I came to a conclusion that something is wrong with \\s+ and \\n.

I have found an alternative in removing white spaces String.replaceAll(" ","");, but I still need a way to remove new lines.

Here is a part of the code

String name = InputString("Enter name: ");
String name1 = name.replaceAll("\\s+","");
   output(name1); // the output function is located in the IBIO which is provided because I'm and IB student

Alright so someone had given me a code that will remove all of the new lines \\n, but if people can give me the reason why my \\s+ not working that would be helpful.

Ali
  • 31
  • 1
  • 3

4 Answers4

4

What does "it doesn't work" mean?

One thing regarding this that people often forget is that class String is immutable. That means that if you call a method like replace() on a String, the original string is not changed; instead, the method returns a new string.

String s = "Hello World";

// Does not change the original string!
s.replaceAll("\\s+", ""); 

// Use this instead to assign s to the new string
s = s.replaceAll("\\s+", "");
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • I have entered a part of the code and you can see I declared the things up there. And someone had just now entered '.replaceAll("(?s)\\s+", "");' which works fine for new line. – Ali Nov 06 '12 at 10:04
3

A possible reason which really stumps people why \\s does not work is if character code 0xa0 (non-breaking space) is present instead of a normal space. The method replaceAll does not catch 0xa0 with a \\s regex. Instead, you would have to do something like:

String name1 = name.replaceAll("\\u00a0",""); 

to replace non-breaking spaces.

demongolem
  • 9,474
  • 36
  • 90
  • 105
0

This line

System.out.println("Multiline \r\n String".replaceAll("\\s+", ""));

prints

MultilineString

Therefore I suggest you post the exact string literal you are experiencing problems with, with the exact output produced.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • The thing is it doesn't output anything, it uses the new string to create a text file. And if I do output(); or System.out.println(); it would give the same result other then that fact that System.out.println(); will give it out in a text file. – Ali Nov 06 '12 at 10:08
  • So your problem lies elsewhere entirely. `output` is not a standard Java function and neither is `InputString`. Do you even know for sure what is the string you are trying to process? – Marko Topolnik Nov 06 '12 at 10:14
  • Yes you are right, as IB students we get a code called the IBIO which does the output function and other functions as well, sorry forgot to mention that – Ali Nov 06 '12 at 10:18
0

to remove newline from String

String s = "Enter name :\n Your name ";
        s = s.replaceAll("\n", "");
Alya'a Gamal
  • 5,624
  • 19
  • 34