How to capitalize/uppercase the first letter of a String in Java?

  1. Accepted Answer

Accepted Answer

Plain Java

String str = "antartica";
String newStr = str.substring(0, 1).toUpperCase() + str.substring(1);




  • str.substring(0, 1) returns the first letter as String
  • .toUpperCase() convert the letter to uppercase
  • + str.substring(1) concatenate with rest of the string
  • Remember Strings are immutable in Java, a new String is returned after each operation, that’s why we are concatenating.

Using Apache Commons String Utils

Apache Commons Library has StringUtils.capitalize() that you could use. It changes the first character of a String to capital, as per title case.

 StringUtils.capitalize("java") = "Java"

You can also use WordUtils.capitalize if you want to capitalize each word of an entire sentence.

WordUtils.capitalize("i am java") = "I Am Java"

Speak Your Mind