Skip to content

Capitalize first character of String in Java example

This Java example shows how to capitalize first character of string or make first letter uppercase in Java using various approaches.

How to capitalize first character or make first letter uppercase?

To capitalize the first character of String or make first letter uppercase, you can use one of the below given approaches.

1) Capitalize the first character of String using the substring and toUpperCase methods of the String class

You can use the substring and toUpperCase methods of the String class to capitalize the first letter of string as given below.

Output

If you want to convert the first character of a string to upper case and all other characters to lower case, change the method as given below.

2) Using the Apache Commons

If you are using the Apache Commons library, you can use the capitalize method of the StringUitls class as given below.

Output

3) Using the charAt method of String class and the toUpperCase method of the Character class

You can also use the charAt method of the String class and the toUpperCase method of the Character class to capitalize the first character of a string as given below.

Output

4) Using a character array

This approach converts string to char array, make the first element of an array to uppercase and then converts the character array back to the string as given below.

This example is a part of the String in Java tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.