What's the best way to replace the first letter of a string in Java?
- by froadie
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?