How to Capitalize the First Letter of a String in Java

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

If you only want to capitalize the first letter of a string named input and leave the rest alone:

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

Now output will have what you want. Check that your input is at least one character long before using this, otherwise you'll get an exception.

How to capitalize the first character of each word in a string

WordUtils.capitalize(str) (from apache commons-text)

(Note: if you need "fOO BAr" to become "Foo Bar", then use capitalizeFully(..) instead)

Capitalizing the first character of a string - Java

It might be easier just to assume that the first letter is always lowercase, then you don't need any checks:

String s         = "some string";
String capitol = Character.toString(s.charAt(0)).toUpperCase();
String newString = capitol + s.substring(1,x);

Capitalizing first and last letter in java using String methods

Code looks fine, just make some changes

String newWord = letter1.toUpperCase()
+ word.substring(1, word.length() - 1)
+ lastletter.toUpperCase();

First Letter - letter1.toUpperCase()

Middle String - word.substring(1, word.length() - 1)

Last Letter - lastletter.toUpperCase();

output

Please Type a word: ankur
The word with first letter capitalized is: AnkuR


Related Topics



Leave a reply



Submit