在android中将CharSequence第一个字母更改为大写

发布于 2024-09-06 09:19:16 字数 415 浏览 9 评论 0原文

它可能看起来很简单,但它有很多错误 我尝试了这种方式:

 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 one也抛出了一个异常

it may seem simple but it posses lots of bugs
I tried this way:

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

and it throws an exception

another try i had was :

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

rhis one also throws an Exception

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗酒趁年少 2024-09-13 09:19:16
/**
 * 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) : "");
}
/**
 * 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) : "");
}
美人如玉 2024-09-13 09:19:16

。 。 。或者在一个数组中完成这一切。这是类似的东西。

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

. . . or do it all in an array. Here's something similar.

    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);
    }
娇纵 2024-09-13 09:19:16

关于字符串不可变

关于您的第一次尝试:

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

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

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

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

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

至于将 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"
    }

如果 s,这当然会抛出 NullPointerException == null。这通常是一种适当的行为。

On String being immutable

Regarding your first attempt:

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

Java strings are immutable. You can't invoke a method on a string instance and expect the method to modify that string. replaceFirst instead returns a new string. This means that these kinds of usage are wrong:

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

Instead, you'd want to do something like this:

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

As for changing the first letter of a CharSequence to uppercase, something like this works (as seen on 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"
    }

This of course will throw NullPointerException if s == null. This is often an appropriate behavior.

套路撩心 2024-09-13 09:19:16

我喜欢使用这个更简单的名称解决方案,其中 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();
}

希望这有帮助。

I like to use this simpler solution for names, where toUp is an array of full names split by (" "):

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

And this modified solution could be used to uppercase only the first letter of a full String, again toUp is a list of strings:

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

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文