Here you can find the source of collapseMultipleNewlinesToOne(String s)
public static String collapseMultipleNewlinesToOne(String s)
//package com.java2s; public class Main { public static String collapseMultipleNewlinesToOne(String s) { return s.replaceAll("\\\n+", "\\\n"); }//from w w w . j a v a 2 s .com public static String replaceAll(String sSource, String sPattern, String sReplacement) { if (sSource == null || sSource.equals("")) { return sSource; } String sResult = new String(sSource); int i = sResult.indexOf(sPattern); while (i != -1) { String sFront = sResult.substring(0, i); String sBack = sResult.substring(i + sPattern.length()); sResult = sFront + sReplacement + sBack; i = sResult.indexOf(sPattern); } return sResult; } }