Java String Whitespace Normalize normalizeWhiteSpace(String input)

Here you can find the source of normalizeWhiteSpace(String input)

Description

normalize White Space

License

Open Source License

Declaration

public static String normalizeWhiteSpace(String input) 

Method Source Code

//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;
    }
}

Related

  1. normaliseWhitespace(String string)
  2. normaliseWhitespace(String string)
  3. normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)
  4. normalizeWhitespace(final String str)
  5. normalizeWhitespace(String dirtyString)
  6. normalizeWhiteSpace(String input)
  7. normalizeWhitespace(String orig)
  8. normalizeWhitespace(String s)
  9. normalizeWhitespace(String source)