字符串包含汉字、数字、字母等校验工具类
import com.github.stuxuhai.jpinyin.PinyinFormat; import com.github.stuxuhai.jpinyin.PinyinHelper; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @ClassName CharsUtil * @Author ZhangRF * @CreateDate 2020/12/19 * @Decription 字符工具类 */ @Slf4j public class CharsUtil { public static String chinese = "[\u4e00-\u9fa5]"; /** * 判断参数是否为字母 * * @param s * @return */ public static boolean isLetter(String s) { if (StringUtils.isEmpty(s)) { return false; } char c = s.charAt(0); try { int i = (int) c; if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) { return true; } else { return false; } } catch (Exception e) { return false; } } /** * 判断参数是否为字母 * * @param s * @return */ public static boolean isLetterChar(String s) { char c = s.charAt(0); if (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) { return true; } else { return false; } } /** * 判断参数是否为数字 * * @param str * @return */ public static boolean isNums(String str) { for (int i = str.length(); --i >= 0; ) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 判断参数是否为数字 * * @param str * @return */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } /** * 判断参数是否是数据类型 */ public static boolean isNumber(String params) { try { BigDecimal bigDecimal = new BigDecimal(params); return true; } catch (Exception e) { return false; } } /** * 判断参数是否为数字 * * @param str * @return */ public static boolean isNum(String str) { for (int i = str.length(); --i >= 0; ) { int chr = str.charAt(i); if (chr < 48 || chr > 57) return false; } return true; } /** * 判断一个字符是否全是汉字 * PS:中文汉字的编码范围:[\u4e00-\u9fa5] * * @param str 需要判断的字符 * @return 是汉字(true), 不是汉字(false) */ public static boolean isChineseChar(String str) { return str.matches(chinese); } /** * 是否存在汉字 * * @param str * @return */ public static boolean isExistChar(String str) { Pattern p = Pattern.compile(chinese); Matcher m = p.matcher(String.valueOf(str)); if (m.find()) { return true; } return false; } /*** * @param str 源字符串 * @param regex 正则表达式 * @return 判断参数是否为汉字 */ public static boolean match(String str, String regex) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.find(); } /*** * 将单个汉字转成拼音 * @param hanzi * @return */ private static String convertSingleHanzi2Pinyin(char hanzi) { if (StringUtil.isEmpty(hanzi)) { return ""; } String[] res; StringBuffer sb = new StringBuffer(); try { res = PinyinHelper.convertToPinyinArray(hanzi, PinyinFormat.WITHOUT_TONE); sb.append(res[0]);//对于多音字,只用第一个拼音 } catch (Exception e) { e.printStackTrace(); log.error("拼音转换错", e); return ""; } return sb.toString(); } /** * 获取汉字首字母及全拼 * * @param params 汉字 * @return */ public static Map<String, String> convertHanzi2Pinyin(String params) { if (org.apache.commons.lang3.StringUtils.isEmpty(params)) { return null; } HashMap<String, String> vo = new HashMap<>(2); StringBuilder all = new StringBuilder(); StringBuilder abbr = new StringBuilder(); //是否为中文 /* * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体 * ^[\u4E00-\u9FA5]+$ 匹配简体 */ String regExp = "^[\u4E00-\u9FA5]+$"; for (int i = 0; i < params.length(); i++) { char unit = params.charAt(i); //是汉字,则转拼音 if (match(String.valueOf(unit), regExp)) { String pinyin = convertSingleHanzi2Pinyin(unit); char[] chars = pinyin.toCharArray(); for (char aChar : chars) { all.append(aChar); } abbr.append(pinyin.charAt(0)); } else { all.append(unit); abbr.append(unit); vo.put("all", all.toString()); vo.put("abbr", abbr.toString()); } } vo.put("all", all.toString()); vo.put("abbr", abbr.toString()); return vo; } /** * 转首字母 * * @param params * @return */ public static String convertInitial(String params) { Map<String, String> map = convertHanzi2Pinyin(params); return map.get("abbr"); }