Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String capitalize - better way

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.

like image 524
IAdapter Avatar asked Oct 08 '09 07:10

IAdapter


People also ask

Should string be capitalized?

The String type is capitalized because it is a class, like Object , not a primitive type like boolean or int (the other types you probably ran across).

How do you capitalize strings?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.

What is the use of string capitalize method?

Definition and Usage The capitalize() method returns a string where the first character is upper case, and the rest is lower case.

What is the difference between title () and capitalize () function on string with the help of an example?

The difference between them is that Python string method title() returns a copy of the string in which the first characters of all the words are capitalized whereas the string method capitalize() returns a copy of the string in which just the first word of the entire string is capitalized.


2 Answers

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
like image 191
Lucero Avatar answered Oct 05 '22 23:10

Lucero


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.)

like image 41
Tom Hawtin - tackline Avatar answered Oct 05 '22 22:10

Tom Hawtin - tackline