Looking for how to capitalize the first letter of a string in Java?

Java tutorial to capitalize first letter of a string. In Java, as of now, the String class does not provide any built-in method to capitalize the first letter.

Thinking like a Java Programmer | The Approach

We have other methods to change the case of the string using

  • To capitalize the whole string toUpperCase() the method is used.

for example-

public class Main {

public static void main(String[] args) {

String string = "hello world";
String resultString = string.toUpperCase();

System.out.print(resultString);

}
}

Output on the console for the above code is

HELLO WORLD

So, now we have a method to make the whole string in upper case, but our problem statement is to capitalize only the first character of the string.

string that need to be capitalized

The string that needs to be capitalized

The expected output should be  Hello world

In order to achieve the expected result, we need to think of some way or workaround to get the 1st character and make it capital, and then concatenate it with the rest of the string. Read more about substring method.

Let’s explore how we can do that-

  • Get the 1st character. (using substring method)

using substring method java

  • Convert the first character to upper case. (using toUpperCase method)

capitalizing the first letter of the string java

  • Get the remaining string and concatenate it with the 1st character that we already capitalized.

Java Program to Capitalize First Letter of String

public class Main {

    public static void main(String[] args) {
    
    String string = "hello world";


    String firstChar=string.substring(0,1);
    
    String remainingString=string.substring(1);

    // conatinate it with the first char and ht remaining string    
    String resultString = firstChar.toUpperCase()+remainingString;
    
    System.out.print(resultString);
    
    }
    }

OUTPUT

Hello world

Hurray!! finally, we achieved it.

The above code shows how we can capitalize the first letter of the string in Java.

What about when we try to assign nothing or Null?

As the substring() method takes the beginning and ending index of the string. Therefore, it will give an Exception.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 1, length 0
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)

We can resolve this at the first instant itself.

Also, let’s make the code reusable and modularize the code reusable.

Creating a Java function to capitalize the first letter of a string

We will make use of the power of encapsulation in java. This will make the code reusable and easy to work with.


    // capitalize method
   public static String capitalize(String str){
        if(str==""|| str==null){
            return str;
        }else{
            return str.substring(0,1).toUpperCase()+str.substring(1);
        }
    } 

Alright, now we can make use of the capitalize() method and use it every time to make the first letter to upper case.

For example:

System.out.print(capitalize("hello, welcome to "));
System.out.print(capitalize("letstacle Academy"));

OUTPUT

Hello, welcome to Letstacle Academy

Conclusion

Capitalizing the first letter in the Java program is most often used. We hope this tutorial helped you to achieve the same. Keep learning keep sharing. Follow us on Facebook and Instagram.