Given a string in camel case, the task is to write a Java program to convert the given string from camel case to snake case and print the modified string.
Examples:
Input: GeeksForGeeks
Output: geeks_for_geeks
Input: CamelCaseToSnakeCase
Output: camel_case_to_snake_case
Method 1: Naive Approach
- First we initialize a variable ‘result’ with an empty string and append the first character (in lower case) to it.
- Now iterate over each character of the string from first index to the last index, if the character is upper case alphabet, we append ‘_’ and the character (in lower case) to result, otherwise just append the character only.
Below is the implementation of the above approach:
Java
class GFG { // Function to covert camel case // string to snake case string public static String camelToSnake(String str) { // Empty String String result = ""; // Append first character(in lower case) // to result string char c = str.charAt(0); result = result + Character.toLowerCase(c); // Tarverse the string from // ist index to last index for (int i = 1; i < str.length(); i++) { char ch = str.charAt(i); // Check if the character is upper case // then append '_' and such character // (in lower case) to result string if (Character.isUpperCase(ch)) { result = result + '_'; result = result + Character.toLowerCase(ch); } // If the character is lower case then // add such character into result string else { result = result + ch; } } // return the result return result; } public static void main(String args[]) { // Given string str String str = "GeeksForGeeks"; // Print the modified string System.out.print(camelToSnake(str)); } } |
geeks_for_geeks
Method 2: Using String.replaceAll method
- The idea is to use the String.replaceAll method to convert given string from camel case to snake case.
- The String.replaceAll method accepts two parameters a regular expression and a replacement string. It replaces the regular expression with the replacement string and prints the modified string.
Below is the implementation of the above approach:
Java
class GFG { // Function to covert camel case // string to snake case string public static String camelToSnake(String str) { // Regular Expression String regex = "([a-z])([A-Z]+)"; // Replacement string String replacement = "$1_$2"; // Replace the given regex // with replacement string // and convert it to lower case. str = str .replaceAll( regex, replacement) .toLowerCase(); // return string return str; } // Driver Code public static void main(String args[]) { // Given string str String str = "GeeksForGeeks"; // Print the modified string System.out.print(camelToSnake(str)); } } |
geeks_for_geeks
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 Snake Case string to Camel 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 a Set of String to a comma separated String in Java
- Convert an ArrayList of String to a String array 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.

