Java tutorial
//package com.java2s; /* * codjo.net * * Common Apache License 2.0 */ public class Main { public static String flatten(String str, boolean removeSpaceChar) { StringBuilder buffer = new StringBuilder(); boolean previousWhite = true; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == '\r' || ch == '\n') { } else if (Character.isWhitespace(ch) || Character.isSpaceChar(ch)) { if (removeSpaceChar) { if (!previousWhite) { buffer.append(' '); } previousWhite = true; } else { buffer.append(ch); } } else { buffer.append(ch); previousWhite = false; } } return buffer.toString(); } }