java将字符串从蛇形命名法转换为驼峰命名法
时间: 2023-06-14 11:07:15 浏览: 138
以下是将蛇形命名法转换为驼峰命名法的Java代码:
```java
public static String snakeToCamel(String snakeCaseString) {
StringBuilder camelCaseString = new StringBuilder();
boolean nextUpperCase = false;
for (int i = 0; i < snakeCaseString.length(); i++) {
char currentChar = snakeCaseString.charAt(i);
if (currentChar == '_') {
nextUpperCase = true;
} else {
if (nextUpperCase) {
camelCaseString.append(Character.toUpperCase(currentChar));
nextUpperCase = false;
} else {
camelCaseString.append(currentChar);
}
}
}
return camelCaseString.toString();
}
```
使用示例:
```java
String snakeCaseString = "hello_world";
String camelCaseString = snakeToCamel(snakeCaseString);
System.out.println(camelCaseString); // 输出 "helloWorld"
```
阅读全文