参考: https://www.cnblogs.com/tianzhijiexian/p/4723880.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
public static final char UNDERLINE='_'; public static String camelToUnderline(String param){ if (param==null||"".equals(param.trim())){ return ""; } int len=param.length(); StringBuilder sb=new StringBuilder(len); for (int i = 0; i < len; i++) { char c=param.charAt(i); if (Character.isUpperCase(c)){ sb.append(UNDERLINE); sb.append(Character.toLowerCase(c)); }else{ sb.append(c); } } return sb.toString(); } public static String underlineToCamel(String param){ if (param==null||"".equals(param.trim())){ return ""; } int len=param.length(); StringBuilder sb=new StringBuilder(len); for (int i = 0; i < len; i++) { char c=param.charAt(i); if (c==UNDERLINE){ if (++i<len){ sb.append(Character.toUpperCase(param.charAt(i))); } }else{ sb.append(c); } } return sb.toString(); } public static String underlineToCamel2(String param){ if (param==null||"".equals(param.trim())){ return ""; } StringBuilder sb=new StringBuilder(param); Matcher mc= Pattern.compile("_").matcher(param); int i=0; while (mc.find()){ int position=mc.end()-(i++); //String.valueOf(Character.toUpperCase(sb.charAt(position))); sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase()); } return sb.toString(); } public static void main(String[] args) { } |