Java String Whitespace Delete deleteWhitespace(String str)

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

Description

delete Whitespace

License

Mozilla Public License

Declaration

public static String deleteWhitespace(String str) 

Method Source Code

//package com.java2s;
//License from project: Mozilla Public License 

public class Main {
    public static String deleteWhitespace(String str) {
        if (isEmpty(str)) {
            return str;
        }//w  w  w  .  j  a v  a  2  s  . co m

        int sz = str.length();

        char[] chs = new char[sz];

        int count = 0;

        for (int i = 0; i < sz; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                chs[count++] = str.charAt(i);
            }
        }

        if (count == sz) {
            return str;
        }

        return new String(chs, 0, count);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static int length(String str) {
        return str == null ? 0 : str.length();
    }

    public static boolean isWhitespace(String str) {
        if (str == null) {
            return false;
        }

        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. deleteWhitespace(final String str)
  2. deleteWhitespace(String s)
  3. deleteWhiteSpace(String s)
  4. deleteWhitespace(String str)
  5. deleteWhitespace(String str)
  6. deleteWhitespace(String str)
  7. deleteWhitespace(String str)
  8. deleteWhitespace(String str)
  9. deleteWhitespace(String str)