Java String with only letters

Introduction

Checks if the String contains only unicode letters.

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



PreviousNext

Related