How to remove a carriage return from a string

String s ="SSR/DANGEROUS GOODS AS PER ATTACHED SHIPPERS
/DECLARATION 1 PACKAGE

NFY
/ACME CONSOLIDATORS"

How to remove the gap between "PACKAGE" and "NFU"?

+5
source share
4 answers

Java String.replaceAllactually accepts a regular expression. You can delete all lines of a new line with:

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

But this will delete all lines of the newline.

Pay attention to double \: so that the string passed to the regular expression parser is \n.

You can also do it that is smarter:

s = s.replaceAll("\\s{2,}", " ");

This will remove all sequences from 2 or more spaces, replacing them with a single space. Since newlines are also spaces, this should do the trick for you.

+23
source

Try this code:

s = s.replaceAll( "PACKAGE\\s*NFY", "PACKAGE NFY" );
+3

? - :

youString.Replace("\r", "")
0
source
string = string.replace(/\s{2,}/g, ' ');
-1
source

All Articles