Java Program to Capitalize the First Letter of Each Word in a String
Last Updated :
07 Feb, 2024
In Java Programming, we can be able to capitalize the first letter of each word in the given String value by using toUpperCase(), toLowerCase(), and another one is substring() method. These methods are available String class in Java and these methods are useful for our problem.
In this article, we will learn how to capitalize the first letter of each word in a String in Java using different methods.
Approach to capitalize the first letter of each word in a String
To capitalize the first letter of each word in a String, first, we have taken one String value then take the first character from each word in the given string then print the result.
- First, we have created one Java class.
- After that, we created one method that is capitalizeWords() which capitalizes the first letter of each word in a String
- After that, we called this method in the main method and then gave input to the method already taken String value.
- Finally, print the result.
Program to capitalize the first letter of each word in a String in Java
1. Using split() and substring() methods
Below is the implementation of split() and substring() methods to capitalize the first letter of each word in a String.
Java
import java.util.Arrays;
public class CapitalizeWordsExample {
public static void main(String[] args) {
String input = "welcome to geeksforgeeks" ;
String result = capitalizeWords(input);
System.out.println( "Input: " + input);
System.out.println( "Output: " + result);
}
public static String capitalizeWords(String input) {
String[] words = input.split( "\\s" );
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(Character.toTitleCase(word.charAt( 0 )))
.append(word.substring( 1 ))
.append( " " );
}
return result.toString().trim();
}
}
|
Output
Input welcome to geeksforgeeks
Output Welcome To Geeksforgeeks
Explanation of the Program:
- In the above program, we have taken one input string value that is welcome to geeksforgeeks.
- After this we split this String into String array by using split() method.
- After that we have taken each character from word by using loop from string array.
- Then change that character from small to capital.
- Then Append the Result to String builder after that it will print the result.
2. Using Java Streams
Java
import java.util.Arrays;
import java.util.stream.Collectors;
public class CapitalizeWordsExample {
public static void main(String[] args) {
String input = "welcome to geeksforgeeks" ;
String result = capitalizeWords(input);
System.out.println( "Input: " + input);
System.out.println( "Output: " + result);
}
public static String capitalizeWords(String input) {
return Arrays.stream(input.split( "\\s" ))
.map(word -> Character.toTitleCase(word.charAt( 0 )) + word.substring( 1 ))
.collect(Collectors.joining( " " ));
}
}
|
Output
Input: welcome to geeksforgeeks
Output: Welcome To Geeksforgeeks
Explanation of the Program:
- In the above program, first we have taken a String Value as input.
- After that converted it into streams by using Array class.
- Then take the first letter from the word and change into Capital letter then added to the collection.
- Finally print the result.
3. Using StringBuilder
In this Java code, we have taken one string value, after that we have taken first letter of each word and converted it into capitalize then print the result.
Java
import java.util.Arrays;
public class CapitalizeWordsExample
{
public static String capitalizeWords(String input)
{
if (input == null || input.isEmpty())
{
return input;
}
String[] words = input.split( "\\s" );
StringBuilder result = new StringBuilder();
for (String word : words)
{
result.append(Character.toUpperCase(word.charAt( 0 )))
.append(word.substring( 1 ).toLowerCase())
.append( " " );
}
return result.toString().trim();
}
public static void main(String[] args)
{
String inputString = "welcome to geeksforgeeks" ;
String capitalizedString = capitalizeWords(inputString);
System.out.println( "Original String: " + inputString);
System.out.println( "Capitalized String: " + capitalizedString);
}
}
|
Output
Original String: welcome to geeksforgeeks
Capitalized String: Welcome To Geeksforgeeks
Explanation of the Program:
- In this above example, first we have taken one string value in main method.
- After that we have used capitalizeWords().
- To this method, we have given String value as input.
- This method can be able to convert capitalize the first letter of each word in a String by using this function.
- In this user defined function first, we have checked the given string is empty or not, if given String is empty this function returns the empty value as a string.
- After that we have converted the String into String array by using split method.
- After that we have created the StringBuilder class object.
Share your thoughts in the comments
Please Login to comment...