There are multiple problems you could be running into, so I'll cover all them in this answer.
First, any methods on String
which appear to modify it actually return a new instance of String
. That means if you do this:
String something = "Hello";
something.replaceAll("l", "");
System.out.println(something); //"Hello"
You'll want to do
something = something.replaceAll("l", "");
Or in your case
mensagem = mensagem.replaceAll("\r\n", " ");
Secondly, there might not be any \r
in the newline, but there is a \n
, or vice versa. Because of that, you want to say that
if \r
exists, remove it. if \n
exists, also remove it
You can do so like this:
mensagem = mensagem.replaceAll("\r*\n*", " ");
The *
operator in a regular expression says to match zero or more of the preceding symbol.