String (String s, boolean addSpace) { while(true) { int lineBreakStart = s.indexOf('\n'); if (lineBreakStart < 0) break; int lineBreakEnd = lineBreakStart+1; int blankLines = 0; while(lineBreakEnd < s.length() && (s.charAt(lineBreakEnd) == ' ' || s.charAt(lineBreakEnd) == '\n')) { if (s.charAt(lineBreakEnd) == '\n') blankLines++; lineBreakEnd++; } if (addSpace) { String separator = blankLines > 0 ? "<p>" : " "; s = s.substring(0, lineBreakStart) + separator + s.substring(lineBreakEnd); } else { s = s.substring(0, lineBreakStart) + s.substring(lineBreakEnd); } } return s; }