views:

139

answers:

4

Given an input String, what is the most efficient way to make just the first character lower case?

I can think of a number of ways to do this. For example, using charAt and subString:

String string= "SomeInputString";
string = Character.toLowerCase(
  string.charAt(0)) + (string.length() > 1 ? string.substring(1) : "");

Or using a char array:

 String string= "SomeInputString";
 char c[] = string.toCharArray();
 c[0] = Character.toLowerCase(c[0]);
 string = new String( c );

I am about to run some tests for the above examples, but I am sure there are many other ways to achieve this effect. What do you recommend?

+6  A: 

Strings in Java are immutable, so either way a new string will be created.

Your first example will probably be slightly more efficient because it only needs to create a new string and not a temporary character array.

Alan Geleynse
A: 

How about adding 32 to the char (as int):

String string= "SomeInputString";
string = (string[0] + 32) 
+ (string.length() > 1 ? string.substring(1) : "");

That way you avoid the function call.

Tamar
Bad idea. It might not be a letter, or it might be a non-ASCII letter.
Adam Crume
Ugh. This makes assumptions about the character encoding use it.
David Gelhar
At the very least, that would not work well if the first character is not alphabetic. I have, however, used a similar approach when coding in C against char arrays with specific values.
Andy
... that's not even valid Java syntax
Yanick Rochon
+3  A: 

When it comes to string manipulation take a look to Jakarta Commons Lang StringUtils

Carlos Tasada
More specifically, the method uncapitalize(java.lang.String) Using StringUtils has the added advantage of not having to worry about NullPointerExceptions in your code.
hexium
I fail at markdown. :(
hexium
Not necessarily the most efficient, but perhaps the clearest, which counts for a lot.
David Gelhar
+2  A: 

Despite a char oriented approach I would suggest a String oriented solution. String.toLowerCase is Locale specific, so I would take this issue into account. String.toLowerCase is to prefer for lower-caseing according to Character.toLowerCase. Also a char oriented solution is not full unicode compatible, because Character.toLowerCase cannot handle supplementary characters.

public static final String uncapitalize(final String originalStr,
            final Locale locale) {
        final int splitIndex = 1;
        final String result;
        if (originalStr.isEmpty()) {
        result = originalStr;
        } else {
        final String first = originalStr.substring(0, splitIndex).toLowerCase(
                locale);
        final String rest = originalStr.substring(splitIndex);
        final StringBuilder uncapStr = new StringBuilder(first).append(rest);
        result = uncapStr.toString();
        }
        return result;
    }

UPDATE: As an example how important the locale setting is let us lowercase I in turkish and german:

System.out.println(uncapitalize("I", new Locale("TR","tr")));
System.out.println(uncapitalize("I", new Locale("DE","de")));

will output two different results:

ı

i

Michael Konietzka