2

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("\r\n", " ");
Community
  • 1
  • 1
Erico Campos
  • 23
  • 1
  • 3
  • Show the code generating the string... For one thing you could try escaping the \\, use `mensagem.replaceAll("\\r\\n", "");` – Codebender Jan 09 '16 at 04:23

2 Answers2

3

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.

samczsun
  • 1,014
  • 6
  • 16
0
mensagem.replaceAll("\r\n|\r|\n", "");
Maxim Z
  • 1
  • 2
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 06 '22 at 16:04