Java String Handling

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. Example also shows how to capitalize first character of a String using various approaches.

How to capitalize first character or make first letter uppercase?

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

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

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

Output

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

2) Capitalize first character of String using Apache Commons

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

Output

3) Capitalize first character of String using charAt method of String class and toUpperCase method of Character class

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

Output

4) Capitalize first character of String using character array

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

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

 

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.