Removing Line Termination Characters from a String

A line termination character sequence is a character or character pair from the set: \n, \r, \r\n, \u0085, \u2028, and \u2029.

Note: The character pair \n\r is considered a two-line terminator character and therefore will be replaced with two spaces.

// Returns a version of the input where all line terminators // are replaced with a space. public static CharSequence removeLineTerminators(CharSequence inputStr) { String patternStr = "(?m)$^|[\\r\\n]+\\z"; String replaceStr = " "; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); return matcher.replaceAll(replaceStr); }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.