1、駝峰轉下劃線操作
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));
}
return sb.toString();
}
2、下劃線轉駝峰
public static String underlineToCamel(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
boolean flag = false;
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (c == UNDERLINE) {
flag = true;
} else {
if (flag) {
sb.append(Character.toUpperCase(param.charAt(i)));
flag = false;
} else {
sb.append(Character.toLowerCase(param.charAt(i)));
}
}
}
return sb.toString();
}
``````java
public static String underlineToCamel(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
boolean flag = false;
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (c == UNDERLINE) {
flag = true;
} else {
if (flag) {
sb.append(Character.toUpperCase(param.charAt(i)));
flag = false;
} else {
sb.append(Character.toLowerCase(param.charAt(i)));
}
}
}
return sb.toString();
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結