Java

How to Capitalize the First Letter of a String in Java?

A String variable contains a group of characters enclosed by double quotes [“  ”]. More specifically, in Java, a string is an object of the String Class that helps to create strings and manipulate them as per requirements. While writing Java programs, programmers often try to capitalize the first letter of a string as it helps the reader to understand that the new line is started from here.

This post will discuss the method related to capitalizing the first letter of Java strings.

How to Capitalize the First Letter of a String in Java?

You can use the “substring()” method in combination with the “toUpperCase()” method of the String class for capitalizing a string’s first letter. Using this approach, first, we will get the first letter of the specified string as a substring, then use the toUpperCase() method to capitalize it. After capitalizing the first letter, concatenate it with the string by utilizing the substring() method.

Syntax
The syntax of the substring() method is:

string.toUpperCase()

Here, the toUpperCase() method will convert the given string into uppercase.

Example 1: Capitalizing First Letter of a String Using toUpperCase()

For instance, we have a string “str” that has “w” as the first letter:

String str = "welcome to linuxhint";

Firstly, we will split the first letter of the created string with the help of the “substring()” and store it in a new string named “s1”:

String s1 = str.substring(0,1);

Then, we will use the “toUpperCase()” method to convert string “s1” to a capital letter and store it in another String type variable “s2”:

String s2 = s1.toUpperCase();

After splitting the first word, we will store the original remaining string in the variable “s3”. Here, we will pass “1” as an argument to the substring() method. Upon doing so, the method will fetch the string from the 2nd index to the last one:

String s3 = str.substring(1);

Finally, we will concatenate the strings “s2” and “s3” string using the concatenation operator “+” and print the values with “System.out.println()” method:

String stng = s2+s3;

System.out.println("The first letter of the string is now capitalized");
System.out.println(stng);

As you can see, the first character of the string is now capitalized as “W”:

Example 2: Handling Empty String While Capitalizing the First Letter of a String

The above example will throw an exception if the string is empty:

Now, we will show you how to solve the above-given issue while capitalizing the first letter of a string.

To do so, we will create a method named “capitalizeStng()” that accepts a string parameter and check whether the string is empty with the help of the “added” if condition. The method returns the string if it is empty; otherwise, the next added code will be executed. Then, the “substring()” method splits the first character from the string and capitalizes it by using toUpperCase() method. Lastly, concatenate both strings using “+” concatenation operator:

public static String capitalizeStng(String str)  
    {  
    if (str == null || str.isEmpty()) {
        return str;  
    }
        String s1 =str.substring(0,1);
        String s2 =s1.toUpperCase();
        String s3 =str.substring(1);
        System.out.println("Actual String: ");
        System.out.println(str);
             String stng = s2+s3;
        System.out.println("The first letter of the string is now capitalized:");
        return stng;
 
    }

Invoke the “capitalizeStng()” method and pass the required string:

public static void main(String[] args) {
    System.out.println(capitalizeStng("welcome to linuxhint"));    
    }

Output

In the above example, the method contains three variables which will take more memory space and time to execute. So, the next example will demonstrate how to perform the same operation while reducing code complexity.

Example 3: Capitalizing the First Letter of a String in Java using User-defined Method

We will now create a user-defined method and perform the above-given procedure by utilizing the built-in Java methods. It will also work the same as above:

public static String capitalizeStng(String str)  
    {  
    if (str == null || str.isEmpty()) {
        return str;  
    }
    else
        return str.substring(0, 1).toUpperCase() + str.substring(1);  
}

Output

We gathered all the necessary information on how to capitalize the first letter of a Java Strings.

Conclusion

For capitalizing string’s first letter you can use the “substring()” method with the toUpperCase() method of the String class. The substring() method splits the first word of a string as a substring and then changes the letter to the upper case using the toUpperCase() method. Finally combine the both resultant strings. The post discussed the method of capitalizing the string’s first letter in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.