Dave's Brain

Browse - Programming Tips - How can I make the first charater of a String lowercase but leave the rest untouched?

Date: 2016may19
Language: Java
Keywords: lcFirst, ucFirst

Q.  How can I make the first charater of a String lowercase but leave the rest untouched?

A.

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; }
In javaScript:
function lcFirst(a) { if (a == null) return ''; return a.substr(0,1).toLowerCase() + a.substr(1); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.