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.

3
  • I assume you're referring to Java, and not JavaScript... Commented Nov 6, 2012 at 9:50
  • 1
    Please see this answer stackoverflow.com/questions/2163045/… It explains what you need to do Commented Nov 6, 2012 at 9:51
  • could you please give your input value as well Commented Dec 28, 2012 at 12:46

4 Answers 4

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+", "");
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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.

3 Comments

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.
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?
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
0

to remove newline from String

String s = "Enter name :\n Your name ";
        s = s.replaceAll("\n", "");

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.