Java String Normalize normalizeWhiteSpace(String str)

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

Description

normalize White Space

License

Open Source License

Declaration

public static String normalizeWhiteSpace(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.Normalizer;

public class Main {
    public static final String SPACE = " ";

    public static String normalizeWhiteSpace(String str) {
        if (str == null) {
            return null;
        }//from  w ww.jav a  2  s . c  om

        String normalized = Normalizer.normalize(str, java.text.Normalizer.Form.NFD);
        int len = normalized.length();
        StringBuffer sb = new StringBuffer();

        int spaceCount = 0;

        for (int i = 0; i < len;) {
            int codePoint = normalized.codePointAt(i);
            boolean whitespace = Character.isWhitespace(codePoint);
            boolean space = Character.isSpaceChar(codePoint);
            int offset = Character.charCount(codePoint);

            i += offset;
            if (space || whitespace) {
                spaceCount++;
                if (spaceCount == 1) {
                    sb.append(SPACE);
                }
            } else {
                spaceCount = 0;
                char[] chars = Character.toChars(codePoint);
                sb.append(chars);
            }
        }

        String result = sb.toString().trim();
        return Normalizer.normalize(result, java.text.Normalizer.Form.NFC);
    }

    public static String trim(String str) {
        return str == null ? null : str.trim();
    }
}

Related

  1. normalizeUnicode(final String str)
  2. normalizeUnicode(String input)
  3. normalizeUnicode(String str)
  4. normalizeUnicodeDiacritics(String text)
  5. normalizeWhitespace(final String str)
  6. normalizeWidth(String text)
  7. normalizeWord(String word)