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());

rhisoneも例外をスローします

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 に答える