1. 程式人生 > >java 判斷一個字串中的字元全是字母

java 判斷一個字串中的字元全是字母

記錄一個方法,用來判斷一個字串中字元是否全為字母

public class MainClass {
    public static void main(String[] args){   
        
        String str = "hhhggdxszfff";  
        boolean is_boolean = isPhonticName(str);
        System.out.println(is_boolean);
}

    
    public static boolean isPhonticName(String str) {
        char[] chars=str.toCharArray();
        boolean isPhontic = false;
        for(int i = 0; i < chars.length; i++) {
            isPhontic = (chars[i] >= 'a' && chars[i] <= 'z') || (chars[i] >= 'A' && chars[i] <= 'Z');
            if (!isPhontic) {
                return false;
            }
        }
        return true;
    }

}