java驼峰转换_Java驼峰和下划线互相转化
packagecom.utils;public classChangeChar {public static final char UNDERLINE = '_';public static voidmain(String[] args) {/*驼峰转下划线*/String str= " itemName bbbbCsss , \n" +
" category ,\n" +
"\tbarCode\t \n" +
"\tvolume\t \n" +
"\tlength\t \n" +
"\twidth\t \n" +可爱女孩图片
"\theight\t \n" +常染色体显性遗传病
"\tunitPrice\t \n" +
"\tshelfLife\t \n" +
"\tshelfLifeUnit\t /\n" +
"\t )";/*下划线转驼峰*/String str2= " ITEM_NAME , \n" +
" CATEGORY ,\n" +
"\tBAR_CODE\t \n" +
"\tVOLUME\t \n" +
"\tLENGTH\t \n" +
"\tWIDTH\t \n" +
"\tHEIGHT\t \n" +
"\tUNIT_PRICE\t \n" +
"\tSHELF_LIFE\t \n" +
"\tSHELF_LIFE_UNIT\t";/*** 测试
**/
/
迈瑞招聘*charType=2 表⽰⼤写, 其他情况都是⼩写*/String STR_ABC= camelToUnderline(str, 2); //下划线⼤写:ABC_DEF
String str_abc = camelToUnderline(str, 1); //下划线⼩写:abc_def
System.out.println("驼峰转化成下划线⼤写 :" +STR_ABC);
System.out.println("驼峰转化成下划线⼩写 :" +str_abc);
String strAbc= underlineToCamel(str2); //下划线转驼峰:abcDef
System.out.println("下划线化成驼峰 :" +strAbc);
}//驼峰转下划线
public staticString camelToUnderline(String param, Integer charType) {if (param == null || "".im())) {return "";
}int len =param.length();
春天美句StringBuilder sb= newStringBuilder(len);for (int i = 0; i < len; i++) {char c =param.charAt(i);if(Charact
er.isUpperCa(c)) {
美词美句摘抄大全sb.append(UNDERLINE);
}if (charType == 2) {
sb.UpperCa(c));//统⼀都转⼤写
} el{
sb.LowerCa(c));//统⼀都转⼩写
}
}String();
}//下划线转驼峰
public staticString underlineToCamel(String param) {if (param == null || "".im())) {return ""; }int len =param.length();
StringBuilder sb= newStringBuilder(len);
幼儿唐诗必背八十首
Boolean flag= fal; //"_" 后转⼤写标志,默认字符前⾯没有"_"
for (int i = 0; i < len; i++) {char c =param.charAt(i);if (c ==UNDERLINE) {
三角战术
日暮苍山远flag= true;continue; //标志设置为true,跳过
} el{if (flag == true) {//表⽰当前字符前⾯是"_" ,当前字符转⼤写
sb.UpperCa(param.charAt(i)));
flag= fal; //重置标识
} el{
sb.LowerCa(param.charAt(i)));
}
}
}String();
}
}