Java String with only whitespace

Introduction

Checks if the String contains only whitespace.

isWhitespace(null)   = false
isWhitespace("")     = true
isWhitespace("  ")   = true
isWhitespace("abc")  = false
isWhitespace("ab2c") = false
isWhitespace("ab-c") = false
public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "";
        System.out.println(isWhitespace(str));
    }//from ww w.  j  a v  a  2  s.  com
    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;
    }

}



PreviousNext

Related