Convert Camel Case String to Snake Case in Java

1. Introduction

Camel case and snake case are two common conventions for the compound naming of variables, functions, or other identifiers in programming. Camel case combines words by capitalizing all words after the first and removing spaces (e.g., camelCaseExample). In contrast, snake case combines words by replacing spaces with underscores and using all lowercase letters (e.g., snake_case_example). Converting between these two cases is a common task in software development, especially when integrating systems that use different naming conventions. This blog post will demonstrate how to convert a string from camel case to snake case in Java.

2. Program Steps

1. Define a camel case string.

2. Iterate through each character in the string.

3. For each uppercase character, append an underscore and the lowercase version of the character to a new string.

4. Handle the first character separately to avoid a leading underscore.

5. Return or print the resulting snake case string.

3. Code Program

public class CamelToSnakeCase {
    public static void main(String[] args) {
        // Step 1: Defining a camel case string
        String camelCaseStr = "camelCaseExample";

        // Step 2: Converting camel case to snake case
        String snakeCaseStr = camelToSnake(camelCaseStr);

        // Step 3: Printing the converted string
        System.out.println("Snake case: " + snakeCaseStr);
    }

    // Method to convert camel case to snake case
    private static String camelToSnake(String str) {
        // StringBuilder to build the result
        StringBuilder result = new StringBuilder();

        // Iterating through each character in the string
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            // If the character is uppercase, append an underscore and convert to lowercase
            if (Character.isUpperCase(ch)) {
                // Avoid adding an underscore at the beginning
                if (i > 0) result.append('_');
                result.append(Character.toLowerCase(ch));
            } else {
                // Append lowercase characters directly
                result.append(ch);
            }
        }

        return result.toString();
    }
}

Output:

Snake case: camel_case_example

Explanation:

1. The program defines a string camelCaseStr in a camel case format.

2. It calls the camelToSnake method, which creates a StringBuilder object to build the snake case string iteratively.

3. As it iterates through each character of the input string, the method checks if the character is uppercase. If so, and it's not the first character, it appends an underscore to the StringBuilder followed by the lowercase equivalent of the uppercase character.

4. If the character is not uppercase (meaning it's already lowercase or a digit), it's appended directly to the StringBuilder.

5. The StringBuilder object, now containing the snake case version of the input string, is converted to a String and returned.

6. This example showcases how to use conditionals and the StringBuilder class in Java to perform case conversion, illustrating a practical string manipulation technique.

Comments