将带下滑线的英文字段转换为驼峰命名及替换字符!

  • 替换字符
 Pattern p = Pattern.compile("cat");
 Matcher m = p.matcher("one cat two cats in the yard");
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, "dog");
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

-将带下滑线的英文字段转换为驼峰式命名

1." _(\\w)"代表匹配"name_value_age_birthday" _v,_a,_b
2." _(\\w\\w)"代表匹配"name_value_age_birthday" _va,_ag,_bi
3.matcher.group(1).toUpperCase()代表从匹配到字符的第一位开始截取va,ag,bi

private static Pattern underLinePattern = Pattern.compile("_(\\w)");
/**
 * 将带下滑线的英文字段转换为驼峰式命名
 * 编辑人:yjj
 * @return
 * 返回值类型: String
 */
public static String convertToCamelCase(String str) {
     str = str.toLowerCase();  
      Matcher matcher = underLinePattern.matcher(str);  
      StringBuffer sb = new StringBuffer();  
      while(matcher.find()){  
          matcher.appendReplacement(sb, matcher.group(1).toUpperCase());  
      }  
      matcher.appendTail(sb);  
      return sb.toString();  
}

你可能感兴趣的:(java,stringbuffer)