Here you can find the source of normalizeWhitespace(final String str)
Parameter | Description |
---|---|
str | String to normalize |
public static String normalizeWhitespace(final String str)
//package com.java2s; /*/*from w ww .j a v a2 s. c om*/ * Copyright (C) 2014 Christian P. Lerch <christian.p.lerch[at]gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.text.CharacterIterator; import java.text.StringCharacterIterator; import javax.annotation.Nullable; public class Main { /** * Utility to normalize whitespace in a String, i.e. collapses any sequence * whitespaces (spaces, tabs, linefeeds etc) into a single ' ' (blank) character. * Note: doesn't trim() the string, however, whitespace at the beginning and end * is normalized too. * @param str String to normalize * @return normalized version of str. */ public static String normalizeWhitespace(final String str) { if (isNullOrEmpty(str)) return str; final StringBuilder buf = new StringBuilder(); final CharacterIterator iter = new StringCharacterIterator(str); boolean inWhitespace = false; // Flag set if we're in a second consecutive whitespace for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (Character.isWhitespace(c)) { if (!inWhitespace) { buf.append(' '); inWhitespace = true; } } else { inWhitespace = false; buf.append(c); } } return buf.toString(); } /** * Checks whether the given CharSequence is null or has zero length. * * <p>Consider normalizing all of your your string references with {@link #nullToEmpty}. * If you do, you can use {@link String#isEmpty()} instead of this * method, and you won't need special null-safe forms of methods like {@link * String#toUpperCase} either. * * @param str the CharSequence reference to check (may be {@code null}) * @return {@code true} if the CharSequence is null or is the empty string */ public static boolean isNullOrEmpty(@Nullable final CharSequence str) { return str == null || str.length() == 0; } public static boolean isNullOrEmpty(@Nullable final String str) { return isNullOrEmpty((CharSequence) str); } }