views:

223

answers:

8

What method of capitalizing is better?

mine:

char[] charArray = string.toCharArray();
charArray[0] = Character.toUpperCase(charArray[0]);
return new String(charArray);

or

commons lang - StringUtils.capitalize:

return new StringBuffer(strLen)
            .append(Character.toTitleCase(str.charAt(0)))
            .append(str.substring(1))
            .toString();

I think mine is better, but i would rather ask.

A: 

Have you timed both?

Honestly, they're equivalent.. so the one that performs better for you is the better one :)

warren
Jesper
Also note that the results may vary depending on the string length.
Lucero
+1  A: 

StringBuffer is declared to be thread safe, so it might be less effective to use it (but one shouldn't bet on it before actually doing some practical tests).

Grzegorz Oledzki
+3  A: 

I guess your version will be a little bit more performant, since it does not allocate as many temporary String objects.

I'd go for this (assuming the string is not empty):

StringBuilder strBuilder = new StringBuilder(string);
strBuilder.setCharAt(0, Character.toUpperCase(strBuilder.charAt(0))));
return strBuilder.toString();

However, note that they are not equivalent in that one uses toUpperCase() and the other uses toTitleCase().

From a forum post:

Titlecase <> uppercase
Unicode defines three kinds of case mapping: lowercase, uppercase, and titlecase. The difference between uppercasing and titlecasing a character or character sequence can be seen in compound characters (that is, a single character that represents a compount of two characters).

For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. (Let us write this compound character using ASCII as "dz".) This character
uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. (Which is
basically "DZ".) But it titlecases to to character U+01F2, LATIN CAPITAL
LETTER D WITH SMALL LETTER Z. (Which we can write "Dz".)

character uppercase titlecase
--------- --------- ---------
dz        DZ        Dz
Lucero
Could you please provide more detail on the difference between toUpperCase() and toTitleCase()?
Tim Büthe
Added some addiotional info.
Lucero
The Apache code was probably written for 1.4 or before. In Sun's implementation back then the Apache code would not create any temporary `char[]` arrays (both `String.substring` and (initially) `StringBuffer.toString` share backing arrays). So the Apache code would gave been, before 2004, faster for large strings.
Tom Hawtin - tackline
A: 

Not sure what the difference between toUpperCase and toTitleCase is, but it looks as if your solution requires one less instantiation of the String class, while the commons lang implementation requires two (substring and toString create new Strings I assume, since String is immutable).

Whether that's "better" (I guess you mean faster) I don't know. Why don't you profile both solutions?

Matthias
A: 

StringBuilder (from Java 5 onwards) is faster than StringBuffer if you don't need it to be thread safe but as others have said you need to test if this is better than your solution in your case.

Chris R
+2  A: 

Performance is equal.

Your code copies the char[] calling string.toCharArray() and new String(charArray).

The apache code on buffer.append(str.substring(1)) and buffer.toString(). The apache code has an extra string instance that has the base char[1,length] content. But this will not be copied when the instance String is created.

Thomas Jung
A: 

look at this question titlecase-conversion . apache FTW.

Nico
+1  A: 

If I were to write a library, I'd try to make sure I got my Unicode right beofre worrying about performance. Off the top of my head:

int len = str.length();
if (len == 0) {
    return str;
}
int head = Character.toUpperCase(str.codePointAt(0));
String tail = str.substring(str.offsetByCodePoints(0, 1));
return new String(new int[] { head }).concat(tail);

(I'd probably also look up the difference between title and upper case before I committed.)

Tom Hawtin - tackline