谁能告诉我如何将snake_case中的字符串转换为:
camel_case转换为camelCase中的字符串,如下所示:
camelCase在Java中?
提前谢谢你。
发布于 2019-12-11 05:42:59
此外,Guava的CaseFormat提供了一个相当整洁的解决方案,允许您从驼峰案例转换为驼峰案例,甚至其他特定案例。
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "camel_case"); // returns camelCase
CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "CAMEL_CASE"); // returns CamelCase
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "camelCase"); // returns CAMEL_CASE
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "CamelCase"); // returns camel-case发布于 2018-05-24 07:46:31
您可以使用toCamelCase工具:
CaseUtils.toCamelCase("camel_case", false, new char[]{'_'}); // returns "camelCase"发布于 2015-12-12 02:38:39
这可能很好用,而且很有效。
public class Test {
public static void main(String[] args) {
String phrase = "always_use_camel_back_notation_in_java";
while(phrase.contains("_")) {
phrase = phrase.replaceFirst("_[a-z]", String.valueOf(Character.toUpperCase(phrase.charAt(phrase.indexOf("_") + 1))));
}
System.out.println(phrase);
}
}https://stackoverflow.com/questions/34228942
复制相似问题