Removing Line Termination Characters from a String
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { 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); } }