'From snake_case to camelCase in Java
Can anyone tell me how to convert a string in snake_case as:
camel_case
to a string in camelCase as:
camelCase
in Java?
Thank you in advance.
Solution 1:[1]
Also Guava's CaseFormat offers quite a neat solution that allows you to transform from and to camel case and even other specific cases.
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
Solution 2:[2]
You can use toCamelCase
util:
CaseUtils.toCamelCase("camel_case", false, new char[]{'_'}); // returns "camelCase"
from Apache Commons Text.
Solution 3:[3]
This might be pretty, and it works
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);
}
}
Solution 4:[4]
This isn't pretty, but it works:
String phrase = "camel_case";
String[] words = phrase.split("_");
String newPhrase = words[0];
for(int i=1; i<words.length; i++){
newPhrase += words[i].substring(0,1).toUpperCase() + words[i].substring(1);
}
Solution 5:[5]
Take a look at oracle's documentation for the string class, notably substring, charAt, indexOf, and toUpperCase
(You can use these as puzzle pieces to solve your problem)
Solution 6:[6]
Using split("_")
and looping over those parts is one way to do it.
Here is an alternative using a StringBuilder
.
String s = "make_me_camel_case";
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '_') {
sb.deleteCharAt(i);
sb.replace(i, i+1, String.valueOf(Character.toUpperCase(sb.charAt(i))));
}
}
System.out.println(sb.toString()); // makeMeCamelCase
Solution 7:[7]
Java SE 8+
Using Java-8 Stream API, we can do it in a single statement.
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String str = "snake_case_to_camel_case";
str = str.indexOf("_") != -1
? str.substring(0, str.indexOf("_")) +
Arrays.stream(str.substring(str.indexOf("_") + 1).split("_"))
.map(s -> Character.toUpperCase(s.charAt(0)) + s.substring(1)).collect(Collectors.joining())
: str;
System.out.println(str);
}
}
Output:
snakeCaseToCamelCase
Java SE 9+
Matcher#replaceAll?(Function<MatchResult, String> replacer)
introduced with Java SE 9, makes it further easy.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "snake_case_to_camel_case";
str = Pattern.compile("_([a-z])")
.matcher(str)
.replaceAll(m -> m.group(1).toUpperCase());
System.out.println(str);
}
}
Output:
snakeCaseToCamelCase
Solution 8:[8]
import org.apache.commons.lang3.text.WordUtils;
public String snakeToCamelCase(String snakeWord) {
String camelWord = snakeWord.replaceAll("_", " ");
camelWord = WordUtils.capitalizeFully(camelWord);
return camelWord.replaceAll(" ", "");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alex |
Solution 2 | Community |
Solution 3 | Roee Gavirel |
Solution 4 | pdem |
Solution 5 | Noam Hacker |
Solution 6 | OneCricketeer |
Solution 7 | |
Solution 8 |