开发者

String capitalize - better way

开发者 https://www.devze.com 2022-12-08 04:09 出处:网络
What method of capitalizing is better? mine: char[] charArray = string.toCharArray(); charArray[0] = Character.toUpperCase(charArray[0]);

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.c开发者_JS百科harAt(0)))
            .append(str.substring(1))
            .toString();

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


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


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


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.


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


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.


Have you timed both?

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


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?


look at this question titlecase-conversion . apache FTW.


/**
     * capitalize the first letter of a string
     * 
     * @param String
     * @return String
     * */
    public static String capitalizeFirst(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        char first = s.charAt(0);
        if (Character.isUpperCase(first)) {
            return s;
        } else {
            return Character.toUpperCase(first) + s.substring(1);
        }
    }


If you only capitalize limited words, you better cache it.

@Test
public void testCase()
{
    String all = "At its base, a shell is simply a macro processor that executes commands. The term macro processor means functionality where text and symbols are expanded to create larger expressions.\n" +
            "\n" +
            "A Unix shell is both a command interpreter and a programming language. As a command interpreter, the shell provides the user interface to the rich set of GNU utilities. The programming language features allow these utilities to be combined. Files containing commands can be created, and become commands themselves. These new commands have the same status as system commands in directories such as /bin, allowing users or groups to establish custom environments to automate their common tasks.\n" +
            "\n" +
            "Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.\n" +
            "\n" +
            "A shell allows execution of GNU commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. The redirection constructs permit fine-grained control of the input and output of those commands. Moreover, the shell allows control over the contents of commands’ environments.\n" +
            "\n" +
            "Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cd, break, continue, and exec cannot be implemented outside of the shell because they directly manipulate the shell itself. The history, getopts, kill, or pwd builtins, among others, could be implemented in separate utilities, but they are more convenient to use as builtin commands. All of the shell builtins are described in subsequent sections.\n" +
            "\n" +
            "While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.\n" +
            "\n" +
            "Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases. Each of these features is described in this manual.";
    String[] split = all.split("[\\W]");

    // 10000000
    // upper Used 606
    // hash Used 114

    // 100000000
    // upper Used 5765
    // hash Used 1101

    HashMap<String, String> cache = Maps.newHashMap();

    long start = System.currentTimeMillis();
    for (int i = 0; i < 100000000; i++)
    {

        String upper = split[i % split.length].toUpperCase();

//            String s = split[i % split.length];
//            String upper = cache.get(s);
//            if (upper == null)
//            {
//                cache.put(s, upper = s.toUpperCase());
// 
//            }
    }
    System.out.println("Used " + (System.currentTimeMillis() - start));
}

The text is picked from here.

Currently, I need to upper case the table name and columns, many many more times, but they are limited.Use the hashMap to cache will be better.

:-)


use this method for capitalizing of string. its totally working without any bug

public String capitalizeString(String value)
{
    String string = value;
    String capitalizedString = "";
    System.out.println(string);
    for(int i = 0; i < string.length(); i++)
    {
        char ch = string.charAt(i);
        if(i == 0 || string.charAt(i-1)==' ')
            ch = Character.toUpperCase(ch);
        capitalizedString += ch;
    }
    return capitalizedString;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消