用java写一个驼峰转下划线的示例
时间: 2024-05-16 10:11:59 浏览: 64
可以的,下面是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
```
希望能够帮助你!
相关问题
java驼峰转下划线
将Java驼峰命名方式转换为下划线命名方式可以使用以下代码:
```java
public static String camelToUnderscore(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)) {
if (i != 0) {
sb.append("_");
}
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
```
使用示例:
```java
String camelCase = "java驼峰转下划线";
String underscore = camelToUnderscore(camelCase);
System.out.println(underscore); // 输出:java_驼峰转下划线
```
java 驼峰转下划线
将Java中的驼峰式命名转换成下划线命名可以使用以下代码:
```java
public static String camelToUnderscore(String str) {
if (str == null || str.isEmpty()) {
return str;
}
StringBuilder result = new StringBuilder(str.substring(0, 1));
for (int i = 1; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isUpperCase(ch)) {
result.append("_").append(Character.toLowerCase(ch));
} else {
result.append(ch);
}
}
return result.toString();
}
```
示例:
输入:`myNameIsJava`
输出:`my_name_is_java`
阅读全文