Make String first letter capital in java

Suresh Atta picture Suresh Atta · Jun 10, 2013 · Viewed 53.9k times · Source

As of now I'm using this code to make my first letter in a string capital

String output = input.substring(0, 1).toUpperCase() + input.substring(1);

This seems very dirty to me ..is there any direct or elegant way..

Answer

arshajii picture arshajii · Jun 10, 2013

How about this:

String output = Character.toUpperCase(input.charAt(0)) + input.substring(1);

I can't think of anything cleaner without using external libraries, but this is definitely better than what you currently have.