This article has shared the specific code of java camel conversion for your reference. The specific content is as follows
Convert "_" to camel, convert camel to "_".
import java.util.regex.Matcher;import java.util.regex.Pattern; /** * Camel conversion* @author Hu Hansan* January 19, 2017 at 4:42:58 pm */public class BeanHump { //Converted dependency characters public static final char UNDERLINE='_'; /** * Convert camel to "_"(userId:user_id) * @param param * @return */ 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(); } /** * Turn "_" into camel(user_id:userId) * @param param * @return */ 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(); } /** * Convert "_" to camel(user_id:userId) * @param param * @return */ public static String underlineToCamel2(String param){ if (param==null||"".equals(param.trim())){ return ""; } StringBuilder sb=new StringBuilder(param); Matcher mc= Pattern.compile(UNDERLINE+"").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(); } /* * Test*/ public static void main(String[] args) { System.out.println(camelToUnderline("userNameAll")); System.out.println(underlineToCamel("user_name_all")); System.out.println(underlineToCamel2("user_name_all")); }}Running results:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.