Answer by Srinivasan Sekar for Remove end of line characters from Java string
You can use unescapeJava from org.apache.commons.text.StringEscapeUtils like below str = "hello\r\njava\r\nbook"; StringEscapeUtils.unescapeJava(str);
View ArticleAnswer by Er.Naved Ali for Remove end of line characters from Java string
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...
View ArticleAnswer by ronicst for Remove end of line characters from Java string
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...
View ArticleAnswer by Vikram Subramanian for Remove end of line characters from Java string
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...
View ArticleAnswer by RazvanM for Remove end of line characters from Java 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...
View ArticleAnswer by Lawrence Dol for Remove end of line characters from Java string
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...
View ArticleAnswer by TofuBeer for Remove end of line characters from Java string
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); } }...
View ArticleAnswer by Rob Lachlan for Remove end of line characters from Java string
Have you tried using the replaceAll method to replace any occurence of \n or \r with the empty String?
View ArticleAnswer by Beep beep for Remove end of line characters from Java string
Given a String str: str = str.replaceAll("\\\\r","") str = str.replaceAll("\\\\n","")
View ArticleRemove end of line characters from Java string
I have string like this "hello java book" I want remove \r and \n from string(hello\r\njava\r\nbook). I want a string as "hellojavabook". How can I do this?
View ArticleAnswer by Abhishek Sengupta for Remove end of line characters from Java string
Hey we can also use this regex solution.String chomp = StringUtils.normalizeSpace(sentence.replaceAll("[\\r\\n]",""));
View ArticleAnswer by Nikhil for Remove end of line characters from Java string
Try below code. It worked for me.str = str.replaceAll("\\r", "");str = str.replaceAll("\\n", "");
View ArticleAnswer by BartusZak for Remove end of line characters from Java string
I went with \\s+ and it removed \r and \n chars for me.\s+ will match one or more whitespace charactersfinal String stringWithWhitespaceChars = "Bart\n\r";final String stringWithoutEscapeChars =...
View Article