I have string like this
"hello
java
book"
I want remove \r
and \n
from String(hello\r\njava\r\nbook)
. I want the result to be "hellojavabook"
. How can I do this?
I have string like this
"hello
java
book"
I want remove \r
and \n
from String(hello\r\njava\r\nbook)
. I want the result to be "hellojavabook"
. How can I do this?
Regex with replaceAll.
public class Main
{
public static void main(final String[] argv)
{
String str;
str = "hello\r\njava\r\nbook";
str = str.replaceAll("(\\r|\\n)", "");
System.out.println(str);
}
}
If you only want to remove \r\n when they are pairs (the above code removes either \r or \n) do this instead:
str = str.replaceAll("\\r\\n", "");
System.getProperty("line.separator");
, instead of hardcoded line breakers. Each OS has its own line separator. –
Pavlish System.getProperty("line.separator");
won't work in these cases. But yes, if you can trust the same-OS origin, it is better to use the system property. –
Cultivate line.separator
? I tried passing "\\" + lineSeparator
in the regex and it did not work –
Ilium public static void main(final String[] argv)
{
String str;
str = "hello\r\n\tjava\r\nbook";
str = str.replaceAll("(\\r|\\n|\\t)", "");
System.out.println(str);
}
It would be useful to add the tabulation in regex too.
If you want to avoid the regex, or must target an earlier JVM, String.replace() will do:
str=str.replace("\r","").replace("\n","");
And to remove a CRLF pair:
str=str.replace("\r\n","");
The latter is more efficient than building a regex to do the same thing. But I think the former will be faster as a regex since the string is only parsed once.
Given a String str:
str = str.replaceAll("\\\\r","")
str = str.replaceAll("\\\\n","")
You can either directly pass line terminator e.g. \n, if you know the line terminator in Windows, Mac or UNIX. Alternatively you can use following code to replace line breaks in either of three major operating system.
str = str.replaceAll("\\r\\n|\\r|\\n", " ");
Above code line will replace line breaks with space in Mac, Windows and Linux. Also you can use line-separator. It will work for all OS. Below is the code snippet for line-separator.
String lineSeparator=System.lineSeparator();
String newString=yourString.replace(lineSeparator, "");
Have you tried using the replaceAll method to replace any occurence of \n or \r with the empty String?
static byte[] discardWhitespace(byte[] data) {
byte groomedData[] = new byte[data.length];
int bytesCopied = 0;
for (int i = 0; i < data.length; i++) {
switch (data[i]) {
case (byte) '\n' :
case (byte) '\r' :
break;
default:
groomedData[bytesCopied++] = data[i];
}
}
byte packedData[] = new byte[bytesCopied];
System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
return packedData;
}
Code found on commons-codec project.
You can use unescapeJava
from org.apache.commons.text.StringEscapeUtils
like below
str = "hello\r\njava\r\nbook";
StringEscapeUtils.unescapeJava(str);
I went with \\s+
and it removed \r
and \n
chars for me.
\s+
will match one or more whitespace characters
final String stringWithWhitespaceChars = "Bart\n\r";
final String stringWithoutEscapeChars = stringWithEscapeChars.replaceAll("\\s+","");
Refer to Regex expressions in Java, \\s vs. \\s+ for in detail informations.
Hey we can also use this regex solution.
String chomp = StringUtils.normalizeSpace(sentence.replaceAll("[\\r\\n]"," "));
Try below code. It worked for me.
str = str.replaceAll("\\r", "");
str = str.replaceAll("\\n", "");
Did you try
string.trim();
This is meant to trim all leading and leaning while spaces in the string. Hope this helps.
Edit: (I was not explicit enough)
So, when you string.split(), you will have a string[] - for each of the strings in the array, do a string.trim() and then append it.
String[] tokens = yourString.split(" ");
StringBuffer buff = new StringBuffer();
for (String token : tokens)
{
buff.append(token.trim());
}
Use stringBuffer/Builder instead of appending in the same string.
© 2022 - 2024 — McMap. All rights reserved.