Date: 2016may19
Update: 2025oct1
Language: Java
Keywords: lcFirst, ucFirst
Q. Java: How can I make the first character of a String lowercase but leave the rest untouched?
A. Here is a full example in Java:
class Demo {
static String lcFirst(final String s) {
if (s == null) return "";
String first = s.substring(0, 1);
String remainder = "";
if (s.length() > 1) {
remainder = s.substring(1);
}
return first.toLowerCase() + remainder;
}
public static final void main(String []args) {
String orig = "Divide by zero";
String out = lcFirst(orig);
System.out.println("orig=" + orig);
System.out.println(" out=" + out);
// Example use
System.out.println("Could not continue because " + out);
}
}
Output:
orig=Divide by zero
out=divide by zero
Could not continue because divide by zero
In javaScript:
function lcFirst(a) {
if (a == null) return '';
return a.substr(0,1).toLowerCase() + a.substr(1);
}