Here you can find the source of normalizeWhiteSpace(String input)
public static String normalizeWhiteSpace(String input)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://from w w w. jav a 2 s. c o m * IBM Corporation - initial API and implementation *******************************************************************************/ public class Main { public static String normalizeWhiteSpace(String input) { if (input == null) { return null; } StringBuffer result = new StringBuffer(); boolean atStart = true; boolean whitespaceToInsert = false; for (int i = 0; i < input.length(); i++) { char next = input.charAt(i); if (Character.isWhitespace(next)) { if (!atStart) { whitespaceToInsert = true; } } else { if (whitespaceToInsert) { result.append(' '); whitespaceToInsert = false; } atStart = false; result.append(next); } } String resString = result.toString(); return resString; } }