看起来很简单,但它有很多错误 我试过这种方式:
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());
这个也抛出一个异常
最佳答案
/**
* 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) : "");
}
关于java - 在android中将CharSequence的首字母更改为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3100526/