Type-1: 

 Let's create a Java function to convert a given string to a title case, which means capitalizing the first letter of each word while keeping the rest of the letters in lowercase.

 We'll also provide a detailed explanation of how the function works:


public class TitleCaseConverter
public static void main(String[] args)
String input = "hello world! how are you?";
String titleCaseString = convertToTitleCase(input); 
 System.out.println("Title Case String: " + titleCaseString); 
 }
public static String convertToTitleCase(String input) {
// Split the input string into words using whitespace as the delimiter 
 String[] words = input.split("\\s+"); 
// Initialize a StringBuilder to store the title case string 
StringBuilder titleCaseBuilder = new StringBuilder(); 
// Process each word in the array 
for (int i = 0; i < words.length; i++) { 
String word = words[i]; 
// Check if the word is not empty 
if (!word.isEmpty()) {
// Convert the first character of the word to uppercase 
char firstChar = Character.toUpperCase(word.charAt(0)); 
// Convert the rest of the characters to lowercase 
String restOfWord = word.substring(1).toLowerCase();
// Append the title case word to the StringBuilder
titleCaseBuilder.append(firstChar).append(restOfWord);
// Append a space after the word, except for the last word
if (i < words.length - 1) { 
 titleCaseBuilder.append(" ");
 } 
 } } 
// Convert the StringBuilder to a string and return the title case string 
return titleCaseBuilder.toString(); 
 }
 }

Explanation:

  1. The program starts with the main method, where we declare and initialize the input variable with the string we want to convert to title case. In this example, the input is set to "hello world! how are you?".
  2. We call the convertToTitleCase method, passing the input as an argument, to obtain the title case string.
  3. In the convertToTitleCase method, we first split the input string into individual words using the split() method with the regular expression "\s+" as the delimiter. This regular expression matches one or more whitespace characters, effectively splitting the sentence into words.
  4. We create a StringBuilder called titleCaseBuilder to store the title case string. A StringBuilder is used to efficiently concatenate strings without creating new objects, making it more efficient than using the + operator on strings.
  5. We then process each word in the words array using a for loop.
  6. Inside the loop, we check if the current word is not empty (i.e., it has at least one character). This check prevents adding unnecessary spaces in cases where there might be multiple consecutive spaces in the input string.
  7. We convert the first character of each word to uppercase using the Character.toUpperCase() method.
  8. We convert the rest of the characters in each word to lowercase using the substring() method to extract the substring starting from the second character and then converting it to lowercase using the toLowerCase() method.
  9. We append the title case word to the titleCaseBuilder using the append() method.
  10. We append a space after each word, except for the last word, to separate the words properly in the title case string.
  11. After processing all the words, the titleCaseBuilder contains the complete title case string.
  12. Finally, we convert the titleCaseBuilder to a string using the toString() method and return the title case string.


By using this approach, we efficiently convert a given string to title case, capitalizing the first letter of each word while keeping the rest of the letters in lowercase. The method handles words separated by different kinds of whitespace characters and properly formats the title case string. This implementation is straightforward and performs well for both short and long strings.

Type-2:

 Another way to convert a given string to a title case is by using the split() method with a regular expression and the StringJoiner class.

 The StringJoiner class helps us join the title case words with spaces efficiently. Let's create a Java function using this approach:


import java.util.StringJoiner; 
public class TitleCaseConverter {
public static void main(String[] args)
String input = "hello world! how are you?";
String titleCaseString = convertToTitleCase(input); 
 System.out.println("Title Case String: " + titleCaseString);
 }
public static String convertToTitleCase(String input) {
// Split the input string into words using whitespace and punctuation as delimiters
String[] words = input.split("[\\s\\p{Punct}]+"); 
// Initialize a StringJoiner to store the title case words
StringJoiner titleCaseJoiner = new StringJoiner(" "); 
// Process each word in the array and add it to the StringJoiner 
for (String word : words) {
if (!word.isEmpty()) { 
// Convert the first character of the word to uppercase
char firstChar = Character.toUpperCase(word.charAt(0)); 
// Convert the rest of the characters to lowercase
String restOfWord = word.substring(1).toLowerCase();
// Append the title case word to the StringJoiner 
 titleCaseJoiner.add(firstChar + restOfWord); } 
 } 
// Convert the StringJoiner to a string and return the title case string 
return titleCaseJoiner.toString();
 } 
}

Explanation:

  1. The code structure and the declaration of the main method are similar to the previous implementations.
  2. We call the convertToTitleCase method, passing the input as an argument, to obtain the title case string.
  3. In the convertToTitleCase method, we split the input string into words using the split() method with the regular expression "[\s\p{Punct}]+". This regular expression matches one or more whitespace characters or punctuation characters, effectively splitting the sentence into words.
  4. We create a StringJoiner called titleCaseJoiner to store the title case words. The StringJoiner is used to efficiently join strings with a specified delimiter (in this case, a space).
  5. We then process each word in the words array using an enhanced for loop.
  6. Inside the loop, we check if the current word is not empty (i.e., it has at least one character). This check prevents adding unnecessary spaces in cases where there might be multiple consecutive spaces or punctuation in the input string.
  7. We convert the first character of each word to uppercase using the Character.toUpperCase() method.
  8. We convert the rest of the characters in each word to lowercase using the substring() method to extract the substring starting from the second character and then converting it to lowercase using the toLowerCase() method.
  9. We append the title case word to the titleCaseJoiner using the add() method.
  10. The StringJoiner efficiently combines the title case words with spaces as specified by the delimiter.
  11. After processing all the words, the titleCaseJoiner contains the complete title case string.
  12. Finally, we convert the titleCaseJoiner to a string using the toString() method and return the title case string.


By using this approach with the StringJoiner class, we efficiently convert a given string to a title case, capitalizing the first letter of each word while keeping the rest of the letters in lowercase. The method handles words separated by different kinds of whitespace characters and punctuation and properly formats the title case string. This implementation is concise and performs well for both short and long strings.