How can I eliminate the line breaks generated by a String?

1

This would be the code that gives me the line break.

miString = miString.replaceFirst("texto a reemplazar", "");
miString = misString.trim();

What happens to me is that when I save it in the .txt it generates that annoying line break. I need to somehow delete line breaks. Try these codes and nothing ....

misString= misString.replaceFirst("\n", "");
misString= misString.replaceAll("\n\r", "");
    
asked by Antonio Olvera 28.08.2018 в 23:49
source

1 answer

3
misString= misString.replaceFirst("\n", "");

As the name (and javadoc) of the function indicates, it will only replace the first occurrence ...

misString= misString.replaceAll("\n\r", "");

This replaces all the occurrences, but the problem is the chain you are looking for.

\ is used to mark escape characters ( escape characters ) that represent non-printable symbols using printable symbols. \n is an escape character representing a new line.

\ is used to "escape" the \ so that it is known to represent the character \ and not that it is a part of an escape character.

So \n is read as "character \ followed by character n ", not as "new line".

The simplest thing, to eliminate them all independently of the end-of-line coding (varies according to the operating system), would be

misString= misString.replaceAll("(\n|\r)", "");

Another option is to use System.getProperty("line.separator") that directly gives you the value of the line separator used by the operating system:

misString= misString.replaceAll(System.getProperty("line.separator", ""));
    
answered by 28.08.2018 / 23:59
source