Given a string in Snake Case, the task is to write a Java program to convert the given string from snake case to camel case and print the modified string.
Examples:
Input: str = “geeks_for_geeks”
Output: GeeksForGeeksInput: str = “snake_case_to_camel_case”
Output: SnakeCaseToCamelCase
Method 1: Using Traversal
- The idea is to first capitalize the first letter of the string.
- Then convert the string to string builder.
- Then traverse the string character by character from the first index to the last index and check if the character is underscored then delete the character and capitalize the next character of the underscore.
- Print the modified string.
Below is the implementation of the above approach:
Java
// Java program for the above approach import java.io.*; class GFG { // Function to convert snake case // to camel case public static String snakeToCamel(String str) { // Capitalize first letter of string str = str.substring(0, 1).toUpperCase() + str.substring(1); // Convert to StringBuilder StringBuilder builder = new StringBuilder(str); // Traverse the string character by // character and remove underscore // and capitalize next letter for (int i = 0; i < builder.length(); i++) { // Check char is underscore if (builder.charAt(i) == '_') { builder.deleteCharAt(i); builder.replace( i, i + 1, String.valueOf( Character.toUpperCase( builder.charAt(i)))); } } // Return in String type return builder.toString(); } // Driver Code public static void main(String[] args) { // Given String String str = "geeks_for_geeks"; // Function Call str = snakeToCamel(str); // Modified String System.out.println(str); } } |
GeeksForGeeks
Method 2: Using String.replaceFirst() method
- The idea is to use String.replaceFirst() method to convert the given string from snake case to camel case.
- First, capitalize the first letter of the string.
- Run a loop till the string contains underscore (_).
- Replace the first occurrence of a letter that present after the underscore to the capitalized form of the next letter of the underscore.
- Print the modified string.
Below is the implementation of the above approach:
Java
// Java program for the above approach class GFG { // Function to convert the string // from snake case to camel case public static String snakeToCamel(String str) { // Capitalize first letter of string str = str.substring(0, 1).toUpperCase() + str.substring(1); // Run a loop till string // string contains underscore while (str.contains("_")) { // Replace the first occurrence // of letter that present after // the underscore, to capitalize // form of next letter of underscore str = str .replaceFirst( "_[a-z]", String.valueOf( Character.toUpperCase( str.charAt( str.indexOf("_") + 1)))); } // Return string return str; } // Driver Code public static void main(String args[]) { // Given string String str = "geeks_for_geeks"; // Print the modified string System.out.print(snakeToCamel(str)); } } |
GeeksForGeeks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert camel case string to snake case in Java
- Sentence Case of a given Camel cased string
- Extract and print words separately from a given Camel Case string
- Camel case of a given sentence
- Snake case of a given sentence
- Convert characters of a string to opposite case
- String in Switch Case in Java
- Lower case to upper case - An interesting fact
- Permute a string by changing case
- Toggle case of a string using Bitwise Operators
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- gOOGLE cASE of a given sentence
- Remove consecutive alphabets which are in same case
- Case-specific Sorting of Strings
- Python | Toggle characters in words having same case
- Case-specific sorting of Strings in O(n) time and O(1) space
- Convert a List of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- Minimum number of operations to move all uppercase characters before all lower case characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

