java有关正则表达式的工具方法集合1
1.获取某字符串中汉字的个数
... private int getChineseCount(String text) { String Reg = "^[\u4e00-\u9fa5]{1}$";// 正则 int result = 0; for (int i = 0; i < text.length(); i++) { String b = Character.toString(text.charAt(i)); if (b.matches(Reg)) result++; } return result; } ...
2.使用正则替换某字符串中所有符合正则的字符串为指定字符串
//将str中的符合正则regu的字符串替换成replaceStr ... private static String replace(String str, String regu, String replaceStr) { Pattern pattern = Pattern.compile(regu); Matcher matcher = pattern.matcher(str); str = matcher.replaceAll(replaceStr); return str; } ...
3.驼峰式转换成大写下划线
... private String humpToLine(String str) { Pattern humpPattern = Pattern.compile("[A-Z]"); Matcher matcher = humpPattern.matcher(str); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase()); } matcher.appendTail(sb); return sb.toString().toUpperCase(); } ...
4.大写下划线转换成驼峰式
... private String lineToHump(String str) { Pattern linePattern = Pattern.compile("_(\\w)"); str = str.toLowerCase(); Matcher matcher = linePattern.matcher(str); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); } matcher.appendTail(sb); return sb.toString(); } ...
相关推荐
flyingssky 2020-08-18
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26