Converting Camel Case and Title Case to Words in Java
Converting Camel Case and Title Case to Words in Java
Java is a great language for manipulating strings, and one area where it really shines is converting text from one format to another. While most Java developers are familiar with the basics of string manipulation, not as many are aware of how to convert certain text formats, such as Camel Case and Title Case, into words. Fortunately, there are several methods for doing this, and in this article we’ll look at how to convert both Camel Case and Title Case to words using Java.
Camel Case
In order to convert a Camel Case string to words, we first need to understand what it is. Camel Case is a type of text formatting which capitalizes the first letter of each word, while all other letters are lowercase. For example: “thisIsACamelCaseString”. In order to convert this to words, we can use the Java String split() method to separate the words based on the capital letters.
String camelCaseString = "thisIsACamelCaseString"; String[] words = camelCaseString.split("(?=[A-Z])"); for (String word : words) { System.out.println(word); }
The output of the above code will be:
- this
- Is
- A
- Camel
- Case
- String
Title Case
The process for converting a Title Case string to words is similar to that of Camel Case. Title Case is a type of text formatting which capitalizes the first letter of each word, and all other letters are lowercase. For example: “This Is A Title Case String”. In order to convert this to words, we can use the Java String split() method to separate the words based on the capital letters.
String titleCaseString = "This Is A Title Case String"; String[] words = titleCaseString.split("(?=[A-Z])"); for (String word : words) { System.out.println(word); }
The output of the above code will be:
- This
- Is
- A
- Title
- Case
- String
As you can see, the processes for converting both Camel Case and Title Case to words are quite similar. The key difference is the syntax for defining the separator for the String split() method. By using this simple technique, you can easily convert between these two formats and manipulate your strings in Java effortlessly.