What's the best way to replace the first letter of a string in Java?

Posted by froadie on Stack Overflow See other posts from Stack Overflow or by froadie
Published on 2010-03-15T13:31:44Z Indexed on 2010/03/15 13:39 UTC
Read the original article Hit count: 192

Filed under:
|

I'm trying to convert the first letter of a string to lowercase. I know there's a capitalize method, but I want to accomplish the opposite. This is the code I used:

value.substring(0,1).toLowerCase() + value.substring(1)

Effective, but feels a bit manual. Are there any other ways to do it? Any better ways? Any Java string functions that do it for you?

I was thinking of using something like a replace function, but Java's replace doesn't accept an index as a parameter. You have to pass the actual character/substring. Another way I can think of doing it is something like:

value.replaceFirst(value.charAt(0), value.charAt(0).toLowerCase())

Except that replaceFirst expects 2 strings, so the value.charAt(0)s would probably need to be replaced with value.substring(0,1)s. Is this any better? Does it matter? Is there any standard way to do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about string-manipulation