Java String with only letters and space ' '

Introduction

Checks if the String contains only unicode letters and space ' '.

isAlphaSpace(null)   = false
isAlphaSpace("")     = true
isAlphaSpace("  ")   = true
isAlphaSpace("abc")  = true
isAlphaSpace("ab c") = true
isAlphaSpace("ab2c") = false
isAlphaSpace("ab-c") = false
public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "demo 2s.com";
        System.out.println(isAlphaSpace(str));
    }/*from ww w .j av a2 s.  c o m*/
    public static boolean isAlphaSpace(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if ((Character.isLetter(str.charAt(i)) == false) && (str.charAt(i) != ' ')) {
                return false;
            }
        }
        return true;
    }
}



PreviousNext

Related