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

Java determines whether it is a letter

尚
Original
2019-11-21 09:15:02 3120browse

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); }

上述代码中判断是否是字母主要使用正则表达式"[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!

Statement:
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