Skip to content
Advertisement

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:

F23C040100C1

100D200000000000

0000

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:

F23C040100C1100D2000000000000000

How can i do that?

I have tried:

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

Advertisement

Answer

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
Advertisement