1.使用substring()
/**
* <p>
* 首字母大写
* 使用String自带的方法substring
*
* @param source 源字符串
* @return {@link String}
*/
public static String toUpperCaseFirstChar(String source) {
if (null == source || source.isEmpty()) return source;
return source.substring(0, 1).toUpperCase() + source.substring(1);
}
2.使用ASCII对照表
/**
* <p>
* 首字母大写
* 使用ASCII对照表以及char与int的类型的自动转化
*
* @param source source
* @return {@link String}
*/
public static String toUpperCaseFirstChar(String source) {
if (null == source || source.isEmpty()) return source;
char[] chs = source.toCharArray();
if (chs[0] > 97 && chs[0] <= 122) chs[0] -= 32;
return new String(chs);
}