Convert Snake case to Pascal case in Java

In this article, we will see what Snake case and Pascal case are and then a short easy code to convert Snake case to Pascal case in Java.

Snake Case: Snake case is a naming style or naming convention in which a phrase or compound word is written with words separated by an underscore(_). The first letter of the compound word can be in upper case or lower case. But conventionally, a compound word is written in Snake case as all small letters with words separated by an underscore. For example:-
“this_is_a_string_in_snake_case”
“information_technology_and_services”

Pascal Case: Pascal case is a naming style or naming convention in which a phrase or compound word is written with each word starting with an upper case letter. There is no space or underscore in between. Pascal case is not to be confused with the Camel case. Camel case is also similar to the Pascal case but the only difference being the first letter of the first word. In the Pascal case, the first letter of the first word of the phrase or compound word is capital whereas it is lower in the Camel case. For example-

Pascal case- “ThisIsAStringInPascalCase”
“InformationTechnologyAndServices”

Camal case- “thisIsAStringInCamelCase”
“informationTechnologyAndServices”

 

Now that we have seen what Snake case and Pascal case are, let’s dive straight into the code to convert Snake case to Pascal case in Java.

 

Code to convert Snake case to Pascal Case in Java

import java.util.Scanner;
import java.util.Arrays;

public class SnakeToPascal {

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter a string in snake case,eg: snake_string: ");
        String snake_string = scan.nextLine();
        String[] parts = snake_string.split("_");
        
        String PascalString = "";
        
        for (int i = 0; i < parts.length; i++){
            
            String string = parts[i];
            String temp = string.substring(0, 1).toUpperCase()+string.substring(1);
            PascalString = PascalString.concat(temp);
            
        }  
        
        System.out.println("\n Pascal case representation of the Snake case string is: ");
        System.out.println(PascalString);
    }
    
}

 

Output:

Enter a string in snake case,eg: snake_string: 
this_is_a_string_in_snake_case
Pascal case representation of the Snake case string is: 
ThisIsAStringInSnakeCase

Let’s check out another example.

Enter a string in snake case,eg: snake_string: 
infromation_technology_and_services
Pascal case representation of the Snake case string is: 
InfromationTechnologyAndServices

Saw how easy it was? It is absolutely fine to use any of these naming styles provided you are consistent with it throughout your code. But it is always helpful to know which programming language uses which naming style predominantly. Java uses Pascal case for class names, constructors, and Camel case for variables, packages, and object attributes and methods.

Want to add your thoughts? Need any further help? Leave a comment below and I will get back to you ASAP 🙂

For further reading:
Constructor equals() method in Java
Thread suspend() method in Java
Overriding toString() method in Java

Leave a Reply

Your email address will not be published.