[2 ways] How to Capitalize the First letter of a String in Java

In this post, we will be learning how to capitalize the first letter of a String in Java. In other words, how to convert the first letter of a String into uppercase. There are two ways to achieve our goal:

1. Using substring() and toUpperCase() methods of String class.
2. Using split() method and enhanced for loop

Read Also: How to find duplicate words in String in Java

First, we will understand the question with the help of the examples:

Input : javahungry
Output: Javahungry


Input : java is the best programming language
Output: Java Is The Best Programming Language

1. Capitalize the First Letter of a String in Java


1.1 Using substring() and toUpperCase() methods of String class


Logic:

a. Break down the givenString into two substrings. One substring will contain the first letter of givenString and the second substring will contain the remaining letters of the givenString.
b. Convert the first letter substring to uppercase using the String class toUpperCase() method.
c. Concatenate the two substrings i.e substring containing the first letter in uppercase and substring containing remaining letters of the givenString.

public class JavaHungry {
    public static void main(String args[]) {
        
        // Original String 
        String givenString = "javahungry";
        
        /*
            Creating two substrings from givenString
            first substring contains the first letter of the givenString
            second substring contains the remaining letters of the givenString
        */
        String firstLetter = givenString.substring(0,1);
        String remainingGivenString = givenString.substring(1);
        
        // Converting the firstLetter to Uppercase using toUpperCase() method
        String firstLetterUpperCase = firstLetter.toUpperCase();
        
        // Last step: Join the two substrings
        System.out.println(firstLetterUpperCase +remainingGivenString);
    }
}

Output:
Javahungry

2.  Capitalize the First Letter of Each Word of a String in Java


2.1 Using split() method and enhanced for loop

Logic:

a. We will convert the givenString into String[] using the split() method.
b. Now, we will iterate through each word in the String[] using an enhanced for loop.
c. Convert each word into two substrings. The first substring will contain the first letter whereas the second substring will contain the remaining letters of the word.
d. First substring containing the first letter will be converted to uppercase using the toUpperCase() method of the String class.
e. Join the two substrings and print the result in the console.

public class JavaHungry {
    public static void main(String args[]) {
        
        // Original String 
        String givenString = "java is a popular programming language";
        
        // Convert the givenString into String array using split method
        String[] words = givenString.split(" ");
        
        for (String word : words)
        {
        /*
            Converting word into two substrings
            first substring contains the first letter using substring method
            second substring contains the remaining letters
        */
        String firstLetter = word.substring(0,1);
        String remainingLetters = word.substring(1);
        
        // Converting firstLetter to Uppercase
        String firstLetterUpperCase = firstLetter.toUpperCase();
        
        // Joining the two substrings
        System.out.print(firstLetterUpperCase +remainingLetters+" ");
        }
    }
}

Output:
Java Is A Popular Programming Language


That's all for today. Please mention in comments in case you have any questions related to how to capitalize the first letter of a String in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry