CodeGym/Java Blog/Strings in Java/Java Capitalize First Letter of a String
Author
Artem Divertitto
Senior Android Developer at United Tech

Java Capitalize First Letter of a String

Published in the Strings in Java group
members

How to capitalize strings in Java?

Java provides a method called toUpperCase() that takes a String as a parameter. It returns a String in an “ALL CAPS” format of the String contents. Here’s a demo snippet for understanding.
String myName = "artem";
System.out.println("myName = " + myName);
System.out.println("myName.toUpperCase() = " + myName.toUpperCase());
Output
myName = artem myName.toUpperCase() = ARTEM

How to capitalize the first letter of a string in Java?

As you can witness in the example above, the Java toUpperCase() capitalizes the entire String. That does not fulfil our requirements. Henceforth, we will design a custom method called capitalize() to only convert the first letter of the String to UpperCase. Below are mentioned method header, its parameters and its return type.

Method Header

Here’s the header for the capitalize() method.
String capitalize(String inputString)
Parameters The capitalize() method takes a string for converting its first letter to capital. Return Type A string with its first letter capitalized.

Algorithm / Steps to capitalize the first letter of the string in Java

Try to run these steps in your mind first before you look at the code.
  1. Get the first letter of the String.
  2. Convert it to an UpperCase letter.
  3. Replace it at the original position in the String.

Example

Here we use a custom created method capitalize(String) to perform the steps mentioned above.
public class Driver {

    public static String capitalize(String inputString) {
        if (inputString == null || inputString.isEmpty()) {
            return inputString;
        }

        // get the first character of the inputString
        char firstLetter = inputString.charAt(0);

        // convert it to an UpperCase letter
        char capitalFirstLetter = Character.toUpperCase(firstLetter);

        // return the output string by updating
        // the first char of the input string
        return capitalFirstLetter + inputString.substring(1);
    }

    public static void main(String[] args) {

        String myName = "artem";
        System.out.println("myName = " + myName);
        System.out.println("capitalize(myName) = " + capitalize(myName) + "\n");

        String myDogName = "leoleo";
        System.out.println("myDogName = " + myDogName);
        System.out.println("capitalize(myDogName) = " + capitalize(myDogName) + "\n");

        String myCarName = "tesla";
        System.out.println("myCarName = " + myCarName);
        System.out.println("capitalize(myCarName) = " + capitalize(myCarName) + "\n");

        String mySchoolName = "nUCES";
        System.out.println("mySchoolName = " + mySchoolName);
        System.out.println("capitalize(mySchoolName) = " + capitalize(mySchoolName) + "\n");

        String myCountryName = "ukraine";
        System.out.println("myCountryName = " + myCountryName);
        System.out.println("capitalize(myCountryName) = " + capitalize(myCountryName) + "\n");

    }
}

Output

myName = artem capitalize(myName) = Artem myDogName = leoleo capitalize(myDogName) = Leoleo myCarName = tesla capitalize(myCarName) = Tesla mySchoolName = nUCES capitalize(mySchoolName) = NUCES myCountryName = ukraine capitalize(myCountryName) = Ukraine

Conclusion

This is a simple illustration of capitalizing the first letter of a String in Java. This is just one implementation. There are multiple other ways to solve the same problem. You’re encouraged to practise this problem with the given solution. After that test yourself out by solving it some other way if you think you have understood the problem well. Test your output. Be brave to manifest your logical and problem-solving abilities. Have a good time learning!

More reading:

Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
aekala Full Stack Developer
3 March 2023, 20:23
This code will not work correctly if the first letter in the word occurs elsewhere in the string. For example if the word was "crunch", the return value would "CrunCh" since replace() replaces all occurrences of the old character with the new one. The following code should work, as well check the boundary case that the input string is empty:
public static String capitalize(String s) {
    if (s.isEmpty()) {
        return s;
    }

    String firstLetterCapitalized = s.substring(0, 1).toUpperCase();
    return firstLetterCapitalized.concat(s.substring(1));
}
Artem Divertitto Senior Android Developer at United Tech
12 December 2023, 10:06
Thank you for your attention to detail, the code has been corrected.