转小驼峰
//下划线对小驼峰命名转换
public class UnderlineToCamelCase {
public static String underlineToCamel(String underlineStr) {
String[] words = underlineStr.split("_");
StringBuilder result = new StringBuilder(words[0]);
// 从第二个单词开始,将每个单词的首字母大写,并添加到结果中
for (int i = 1; i < words.length; i++) {
String word = words[i];
if (word.length() > 0) {
result.append(Character.toUpperCase(word.charAt(0)));
if (word.length() > 1) {
result.append(word.substring(1));
}
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(underlineToCamel("user_name"));
}
}
转大驼峰
public class UnderlineToBigCamelCase {
public static String underlineToBigCamel(String underlineStr) {
StringBuilder result = new StringBuilder();
String[] words = underlineStr.split("_");
// 遍历每个单词
for (String word : words) {
if (word.length() > 0) {
result.append(Character.toUpperCase(word.charAt(0)));
if (word.length() > 1) {
result.append(word.substring(1));
}
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(underlineToBigCamel("user_name"));
}
}