【问题标题】:changing CharSequence first letter to upper case in android在android中将CharSequence的第一个字母更改为大写
【发布时间】:2011-03-07 05:51:30
【问题描述】:

它看起来很简单,但它有很多错误 我试过这样:

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

他也抛出异常

【问题讨论】:

  • 你得到的异常可能会帮助人们回答这个问题

标签: java android string uppercase


【解决方案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) : "");
}

【讨论】:

  • 我的问题很简单,都是关于空字符串的
【解决方案2】:

关于字符串是不可变的

关于你的第一次尝试:

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

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

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

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

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

至于将CharSequence 的第一个字母改为大写,类似这样的方法(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"
    }

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2019-06-02
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        • 1970-01-01
        • 1970-01-01
        • 2018-07-08
        • 2023-03-09
        • 1970-01-01
        相关资源
        最近更新 更多