string - From snake_case to camelCase in Java

String - From snake_case to camelCase in Java

To convert a string from snake_case to camelCase in Java, you can use a combination of String manipulation and regular expressions. Here's a method to achieve this:

public class SnakeToCamel { public static String snakeToCamel(String snakeCase) { StringBuilder camelCase = new StringBuilder(); boolean capitalizeNext = false; for (char c : snakeCase.toCharArray()) { if (c == '_') { capitalizeNext = true; } else if (capitalizeNext) { camelCase.append(Character.toUpperCase(c)); capitalizeNext = false; } else { camelCase.append(c); } } return camelCase.toString(); } public static void main(String[] args) { String snakeCase = "hello_world_test"; String camelCase = snakeToCamel(snakeCase); System.out.println("CamelCase: " + camelCase); } } 

Output:

CamelCase: helloWorldTest 

Explanation:

  • The snakeToCamel method iterates over each character of the input string.
  • If a character is an underscore (_), it sets a flag to capitalize the next character.
  • If the flag is set, it appends the uppercase version of the current character and resets the flag.
  • Otherwise, it appends the character as is.
  • The main method demonstrates the usage of the snakeToCamel method.

Examples

  1. Convert snake_case to camelCase Using String Operations

    Description: Convert a snake_case string to camelCase using basic string operations in Java.

    Code:

    public static String snakeToCamel(String snakeCase) { StringBuilder camelCase = new StringBuilder(); boolean nextUpperCase = false; for (int i = 0; i < snakeCase.length(); i++) { char currentChar = snakeCase.charAt(i); if (currentChar == '_') { nextUpperCase = true; } else { if (nextUpperCase) { camelCase.append(Character.toUpperCase(currentChar)); nextUpperCase = false; } else { camelCase.append(currentChar); } } } return camelCase.toString(); } 

    Explanation: This method iterates through the characters of the input snakeCase string, converting it to camelCase by capitalizing the first character after each underscore (_).

  2. Convert snake_case to camelCase Using Apache Commons Text

    Description: Utilize Apache Commons Text library to convert snake_case to camelCase in Java.

    Code:

    import org.apache.commons.text.WordUtils; public static String snakeToCamel(String snakeCase) { return WordUtils.capitalizeFully(snakeCase, '_').replace("_", ""); } 

    Explanation: Apache Commons Text library provides a utility method WordUtils.capitalizeFully() to capitalize each word separated by underscores (_). We then remove underscores to convert to camelCase.

  3. Convert snake_case to camelCase Using Regular Expressions

    Description: Convert snake_case to camelCase using regular expressions in Java.

    Code:

    public static String snakeToCamel(String snakeCase) { StringBuilder camelCase = new StringBuilder(); boolean capitalizeNext = false; for (int i = 0; i < snakeCase.length(); i++) { char currentChar = snakeCase.charAt(i); if (currentChar == '_') { capitalizeNext = true; } else { if (capitalizeNext) { camelCase.append(Character.toUpperCase(currentChar)); capitalizeNext = false; } else { camelCase.append(Character.toLowerCase(currentChar)); } } } return camelCase.toString(); } 

    Explanation: This approach uses a loop to iterate through each character of snakeCase, converting it to camelCase by capitalizing the first character after each underscore (_).

  4. Convert snake_case to camelCase Using Stream API

    Description: Convert snake_case to camelCase using Java Stream API.

    Code:

    public static String snakeToCamel(String snakeCase) { return Arrays.stream(snakeCase.split("_")) .map(s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase()) .reduce("", String::concat); } 

    Explanation: This code splits the snakeCase string by underscores (_), capitalizes the first character of each part, and then concatenates them to form camelCase.

  5. Convert snake_case to camelCase Using StringJoiner

    Description: Convert snake_case to camelCase using Java StringJoiner.

    Code:

    import java.util.StringJoiner; public static String snakeToCamel(String snakeCase) { String[] parts = snakeCase.split("_"); StringJoiner camelCase = new StringJoiner(""); for (String part : parts) { camelCase.add(part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase()); } return camelCase.toString(); } 

    Explanation: This code splits the snakeCase string by underscores (_), capitalizes the first character of each part, and then joins them using StringJoiner to form camelCase.

  6. Convert snake_case to camelCase Using Apache Commons Lang

    Description: Utilize Apache Commons Lang library to convert snake_case to camelCase in Java.

    Code:

    import org.apache.commons.lang3.StringUtils; public static String snakeToCamel(String snakeCase) { return StringUtils.capitalize(StringUtils.join(StringUtils.split(snakeCase, '_'), "")); } 

    Explanation: Apache Commons Lang provides utility methods like StringUtils.split() and StringUtils.capitalize() to split the snakeCase string by underscores and capitalize each part to form camelCase.

  7. Convert snake_case to camelCase Using Java Streams and Collectors

    Description: Convert snake_case to camelCase using Java Streams and Collectors.

    Code:

    import java.util.Arrays; import java.util.stream.Collectors; public static String snakeToCamel(String snakeCase) { return Arrays.stream(snakeCase.split("_")) .map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase()) .collect(Collectors.joining()); } 

    Explanation: This code splits the snakeCase string by underscores (_), capitalizes the first character of each part, and then collects them into a single camelCase string using Java Streams and Collectors.

  8. Convert snake_case to camelCase Using Guava

    Description: Convert snake_case to camelCase using Google Guava library in Java.

    Code:

    import com.google.common.base.CaseFormat; public static String snakeToCamel(String snakeCase) { return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, snakeCase); } 

    Explanation: Google Guava library provides CaseFormat class with constants for various naming conventions. Here, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, snakeCase) converts snakeCase to camelCase.

  9. Convert snake_case to camelCase Using StringBuilder and Char Manipulation

    Description: Convert snake_case to camelCase using StringBuilder and character manipulation in Java.

    Code:

    public static String snakeToCamel(String snakeCase) { StringBuilder camelCase = new StringBuilder(); boolean toUpperCase = false; for (char c : snakeCase.toCharArray()) { if (c == '_') { toUpperCase = true; } else { if (toUpperCase) { camelCase.append(Character.toUpperCase(c)); toUpperCase = false; } else { camelCase.append(Character.toLowerCase(c)); } } } return camelCase.toString(); } 

    Explanation: This method iterates through each character of snakeCase, converting it to camelCase by capitalizing the first character after each underscore (_) and appending to camelCase.

  10. Convert snake_case to camelCase Using Regular Expressions and Lambda

    Description: Convert snake_case to camelCase using Java 8 lambda expression and regular expressions.

    Code:

    public static String snakeToCamel(String snakeCase) { return Arrays.stream(snakeCase.split("_")) .reduce("", (acc, part) -> acc + Character.toUpperCase(part.charAt(0)) + part.substring(1).toLowerCase()); } 

    Explanation: This code splits the snakeCase string by underscores (_), converts each part to camelCase by capitalizing the first character, and then concatenates them using Java 8 lambda expression and reduce() function.


More Tags

mql4 save-as inner-classes activeadmin master-detail ntfs format-conversion zend-framework3 netty enumerate

More Programming Questions

More Organic chemistry Calculators

More Date and Time Calculators

More Electrochemistry Calculators

More Statistics Calculators