Here you can find the source of normalizeWhitespace(String s)
Parameter | Description |
---|---|
s | the string, or <code>null</code>. |
null
.
public static String normalizeWhitespace(String s)
//package com.java2s; // See the COPYRIGHT file for copyright and license information public class Main { /**/* w w w .ja v a2 s . c o m*/ * Removes all leading and trailing whitespace from a string, and replaces all internal whitespace with a single space character. If <code>null</code> is passed, then <code>""</code> is returned. * * @param s * the string, or <code>null</code>. * @return the string with all leading and trailing whitespace removed and all internal whitespace normalized, never <code>null</code>. */ public static String normalizeWhitespace(String s) { String normalized = ""; if (s != null) { s = s.trim(); boolean prevIsWhitespace = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); boolean thisIsWhitespace = (c <= 0x20); if (!(thisIsWhitespace && prevIsWhitespace)) { if (thisIsWhitespace) { normalized += ' '; prevIsWhitespace = true; } else { normalized += c; prevIsWhitespace = false; } } } } return normalized; } }