将给定字符串每一个首字母大写
例:“this is a lesson about java” --> “This Is A Lesson About Java”
public String firstLetterToUpperCase(String str){
String result = ""; //用于接收最终拼接完整的字符串
//将完整的字符串按照空格拆分
String[] value = str.split(" ");
//循环处理每一个单词 截取首字母变大写 截取其余字母 整体拼接
for(int i = 0; i < value.length; i++){
//获取每一个单词
String word = value[i];
//首字母大写
String firstLetter = word.substring(0 , 1).toUpperCase();
String otherLetters = word.substring(1); //其余的字母
//拼接 注意String 的不可变特性
result = result.concat(firstLetter.concat(otherLetters) + “ ”);
//当然啦 我们也可以在一开始的时候就用StringBuilder那一用append()方法
}
return result.trim(); //去掉最后多余的那个空格
}