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
How to Set Thousands Separator in Java
Listening for Input Without Focus in Java
What Is Difference Between Collection.Stream().Foreach() and Collection.Foreach()
Androidruntime Error: Parcel: Unable to Marshal Value
What Exactly Does Urlconnection.Setdooutput() Affect
How to Use Interceptor to Add Headers in Retrofit 2.0
Detecting State Changes Made to the Bluetoothadapter
How to Make an Array of Arrays in Java
Regex Look-Behind Without Obvious Maximum Length in Java
When Should We Use Observer and Observable
Javafx - Border Radius <-> Background Color
Android How to Listen for Volume Button Events