8

它可能看起来很简单,但它有很多我尝试过的错误:

 String s = gameList[0].toString();
s.replaceFirst(String.valueOf(s.charAt(0)),String.valueOf(Character.toUpperCase(s.charAt(0))) );

它抛出一个异常

我的另一个尝试是:

String s = gameList[0].toString();
char c = Character.toUpperCase(gameList[0].charAt(0));
gameList[0] = s.subSequence(1, s.length());

rhis 也抛出异常

4

4 回答 4

15
/**
 * returns the string, the first char lowercase
 *
 * @param target
 * @return
 */
public final static String asLowerCaseFirstChar(final String target) {

    if ((target == null) || (target.length() == 0)) {
        return target; // You could omit this check and simply live with an
                       // exception if you like
    }
    return Character.toLowerCase(target.charAt(0))
            + (target.length() > 1 ? target.substring(1) : "");
}

/**
 * returns the string, the first char uppercase
 *
 * @param target
 * @return
 */
public final static String asUpperCaseFirstChar(final String target) {

    if ((target == null) || (target.length() == 0)) {
        return target; // You could omit this check and simply live with an
                       // exception if you like
    }
    return Character.toUpperCase(target.charAt(0))
            + (target.length() > 1 ? target.substring(1) : "");
}
于 2010-06-23T09:55:36.590 回答
10

. . . 或在数组中完成所有操作。这里有类似的东西。

    String titleize(String source){
        boolean cap = true;
        char[]  out = source.toCharArray();
        int i, len = source.length();
        for(i=0; i<len; i++){
            if(Character.isWhitespace(out[i])){
                cap = true;
                continue;
            }
            if(cap){
                out[i] = Character.toUpperCase(out[i]);
                cap = false;
            }
        }
        return new String(out);
    }
于 2012-10-23T14:43:28.930 回答
2

字符串是不可变的

关于您的第一次尝试:

String s = gameList[0].toString();
s.replaceFirst(...);

Java 字符串是不可变的。您不能在字符串实例上调用方法并期望该方法修改该字符串。replaceFirst而是返回一个字符串。这意味着这些用法是错误的:

s1.trim();
s2.replace("x", "y");

相反,你想做这样的事情:

s1 = s1.trim();
s2 = s2.replace("x", "y");

至于将 a 的第一个字母更改CharSequence为大写,类似这样的工作(如在 ideone.com 上所见):

    static public CharSequence upperFirst(CharSequence s) {
        if (s.length() == 0) {
            return s;
        } else {
            return Character.toUpperCase(s.charAt(0))
                + s.subSequence(1, s.length()).toString();
        }
    }
    public static void main(String[] args) {
        String[] tests = {
            "xyz", "123 abc", "x", ""
        };
        for (String s : tests) {
            System.out.printf("[%s]->[%s]%n", s, upperFirst(s));
        }
        // [xyz]->[Xyz]
        // [123 abc]->[123 abc]
        // [x]->[X]
        // []->[]

        StringBuilder sb = new StringBuilder("blah");
        System.out.println(upperFirst(sb));
        // prints "Blah"
    }

这当然会抛出NullPointerExceptionif s == null。这通常是一种适当的行为。

于 2010-06-23T13:05:23.270 回答
0

我喜欢对名称使用这种更简单的解决方案,其中 toUp 是由 (" ") 分隔的全名数组:

for (String name : toUp) {
    result = result + Character.toUpperCase(name.charAt(0)) + 
             name.substring(1).toLowerCase() + " ";
}

并且这个修改后的解决方案可用于仅将完整字符串的第一个字母大写,toUp 也是一个字符串列表:

for (String line : toUp) {
    result = result + Character.toUpperCase(line.charAt(0)) + 
             line.substring(1).toLowerCase();
}

希望这可以帮助。

于 2013-05-31T18:52:33.857 回答