Skip to content

How can i put a string in one line

i’m doing some conversion, from Hex to Ascii, when i convert the string, i got the following example:




I know that the string is coming like this, because of the base 16, but i want too put it in just one line, like this:


How can i do that?

I have tried:

mensagem.replaceAll("rn", " ");



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("rn", " ");

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful