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.