Java正则表达式 Java正则表达式工具方法汇总
龙谷情Sinoam 人气:0想了解Java正则表达式工具方法汇总的相关内容吗,龙谷情Sinoam在本文为您仔细讲解Java正则表达式的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,正则表达式,工具方法,下面大家一起来学习吧。
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(); } ...
加载全部内容