• Register
294 points
‚óŹ3

Java Program to convert snake case to camel case and camel case to snake case .

 
Fist let’s understand What is snake case ? 
 
Snake case string is type of string in which every space is separated by special character .
 
For example :- 
 
Simple String  :-  this is my book
Snake Case :- this-is-my-book
 
   Thought about converting first convert into char array then if  next element is ‘_’ the skip other wise convert next alphabet with upper case.
 
Let’s write some program  ,
 
public static String snakeToCamel(String snakeCaseString) {
            boolean isPreviousElWasUnderscore = false;
            char[] snakeStringArray = snakeCaseString.toCharArray();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < snakeStringArray.length; i++) {
                if (snakeStringArray[i] == '_') {
                    isPreviousElWasUnderscore = true;
                } else if (!isPreviousElWasUnderscore) {
                    builder.append(snakeStringArray[i]);
                } else {
                    builder.append(String.valueOf(snakeStringArray[i]).toUpperCase());
                    isPreviousElWasUnderscore = false;
                }
            }
            return builder.toString();
        }

 

Camle case is the String which replace space with capitallizing  successive alphabet.

 

Simple String :- this is my book

Came String :- thisIsMyBook

 

Now let’s see program for converting camel case to snake case string :-

 

    public static String camelToSnake(String camelCaseString) {
        char[] camelStringArray = camelCaseString.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < camelStringArray.length; i++) {
            if (Character.isUpperCase(camelStringArray[i])) {
                String s = String.valueOf(camelStringArray[i]).toLowerCase();
                builder.append("_" + s);
            } else {
                builder.append(camelStringArray[i]);
            }
        }
        return builder.toString();
    }

 

You can also visit other Spring boot blogs of mine from below links :- 

Bean Life cycle – https://kodlogs.com/blog/9239/spring-bean-life-cycle-and-creation-of-bean-with-bean-scope

Various Annotations – https://kodlogs.com/blog/9763/framework-annotations-%40autowire-%40component-%40configuration

Postgre CRUD – https://kodlogs.com/blog/9348/create-web-application-in-spring-boot-with-postgresql-part

 

 

4 Comments

comment:

thanks for sharing this.
Interesting information&hellip; Keep it up, thank you.
Comment:)

Hey Darshan! There is a lot of information in this article. For example, we have the variable 

# first name. If you make it a variable, you have to combine it in snake case writing and camel case writing.

first name ------->simple writing 

first_name-------->snake writing 

FirstName-------->camel writing 

Please correct the word camel in your article.

Today you have taught me something new. Great job. Continue to post :)