Home > Java > JavaBase > Java determines whether it is a letter

Java determines whether it is a letter

Release: 2019-11-21 09:15:02
Original
3228 people have browsed it

Java determines whether it is a letter

java判断是否是字母的方法代码:

  /** 速度快
     * 判断是否为整数
     * @param str 传入字符串
     * @return 是整数返回true,否则返回false
     */
     public static boolean isInteger(String str){
         Pattern pattern=Pattern.compile("^[-\\+]?[\\d]*$");
         return pattern.matcher(str).matches();
     }
    /**
     * 判断是否是字母
     * @param str 传入字符串
     * @return 是字母返回true,否则返回false
     */
    public boolean is_alpha(String str) {
        if(str==null) return false;
        return str.matches("[a-zA-Z]+");
    }
    /**
     * 判断是否是字母或者数字
     * @param str 传入字符串
     * @return 是字母返回true,否则返回false
     */
    public  static boolean isLetterDigit(String str) {
        String regex = "^[a-z0-9A-Z]+$";
        return str.matches(regex);
 
}
Copy after login

上述代码中判断是否是字母主要使用正则表达式"[a-zA-Z]+",此表达式可以匹配字符串中所有字母,是字母返回true,否则返回false。

更多java知识请关注java基础教程

The above is the detailed content of Java determines whether it is a letter. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template