Looking for easy way in java to change only the first letter to upper case of a string.
For example, I have a String DRIVER
, how can I make it Driver
with java
Looking for easy way in java to change only the first letter to upper case of a string.
For example, I have a String DRIVER
, how can I make it Driver
with java
You could try this:
String d = "DRIVER";
d = d.substring(0,1) + d.substring(1).toLowerCase();
Edit:
see also StringUtils.capitalize(), like so:
d = StringUtils.capitalize(d.toLowerCase());
WordUtils.capitalize(string);
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html
String str = "DRIVER";
String strFirst = str.substring(0,1);
str = strFirst + str.substring(1).toLowerCase();
public static void main(String[] args) {
String txt = "DRIVER";
txt = txt.substring(0,1).toUpperCase() + txt.substring(1).toLowerCase();
System.out.print(txt);
}
I would use CapitalizeFully()
String s = "DRIVER";
WordUtils.capitalizeFully(s);
s
would hold "Driver"
capitalize()
only changes the first character to a capital, it doesn't touch the others.
I understand CapitalizeFully()
changes the first char to a capitol and the other to a lower case.
By the way there are lots of other great functions in the Apache Commons Lang Library.
I am using Springs so I can do:
String d = "DRIVER";
d = StringUtils.capitalize(d.toLowerCase());