Java将多个连续的换行符替换成一个换行符

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public static String replaceLineBlanks(String str) {
        String result = "";

        if (str == null || "".equals(str)) {
            Pattern p = Pattern.compile("(r?n(\s*r?n)+)");
            Matcher m = p.matcher(str);
            result = m.replaceAll("rn");
        }

        return result;
}

发表评论