用java写一个驼峰转下划线的示例
时间: 2024-05-16 14:11:59 浏览: 60
java中驼峰与下划线的写法互转
5星 · 资源好评率100%
可以的,下面是Java示例代码:
```java
public static String camelToUnderscore(String camelString) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < camelString.length(); i++) {
char c = camelString.charAt(i);
if (Character.isUpperCase(c)) {
sb.append("_").append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
```
使用示例:
```java
String camelString = "javaIsFun";
String underscoreString = camelToUnderscore(camelString);
System.out.println(underscoreString); // 输出:java_is_fun
```
希望能够帮助你!
阅读全文