Fix Line Separator for string in Java
Description
The following code shows how to fix Line Separator.
Example
//w ww . java 2s . c om
import java.io.UnsupportedEncodingException;
public class Main {
public static void main(String[] argv) throws UnsupportedEncodingException {
String lines = "java2s.com\n\n\n\nabc";
System.out.println(lines);
System.out.println(fixLineSeparator(lines));
}
public static String fixLineSeparator(String xml) {
if ("\r\n".equals(System.getProperty("line.separator"))) {
xml = xml.replaceAll("\r[^\n]", System.getProperty("line.separator"));
} else {
xml = xml.replaceAll("\r\n", System.getProperty("line.separator"));
}
return xml;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »