Java - Write code to remove Invisible char by char type in Character

Requirements

Write code to remove Invisible

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com asdf \y \u \t\n";
        System.out.println(removeInvisible(str));
    }/*from w  w w.j av a  2 s .c om*/

    public static String removeInvisible(String str) {
        if (null != str) {
            str = str.trim();
            StringBuilder newString = new StringBuilder(str.length());
            for (int offset = 0; offset < str.length();) {
                int codePoint = str.codePointAt(offset);
                offset += Character.charCount(codePoint);
                switch (Character.getType(codePoint)) {
                case Character.CONTROL: // \p{Cc}
                case Character.FORMAT: // \p{Cf}
                case Character.PRIVATE_USE: // \p{Co}
                case Character.SURROGATE: // \p{Cs}
                case Character.UNASSIGNED: // \p{Cn}
                case Character.OTHER_SYMBOL: // \OTHER_SYMBOL
                case Character.SPACE_SEPARATOR: // \SPACE_SEPARATOR
                    newString.append(' ');
                    break;
                default:
                    newString.append(Character.toChars(codePoint));
                    break;
                }
            }
            return newString.toString();
        } else {
            return "";
        }
    }
}