将一个字符串首字母转化为大写
public class StringExam1 {
public static void main(String[] args) {
String a = "nI HaO heLlo wORLD";
String newLowerString = a.toLowerCase();
String aa = a.substring(0,1);
String bb = newLowerString.substring(1,newLowerString.length());
String up = aa.toUpperCase();
System.out.println(up+bb);
}
}
运行效果
![运行效果](https://i-blog.csdnimg.cn/blog_migrate/9064dba5f13bce1e24825c591b62de33.png)
将每个单词的首字母转化为大写
public class Aa {
public static void main(String[] args) {
String str = "nI HaO heLlo wORLD";
String str1 = str.toLowerCase();
String s ="";
String aa[] = str1.split(" ");
for (int i = 0;i<aa.length;i++){
aa[i]=aa[i].replace(aa[i].substring(0,1), aa[i].substring(0,1).toUpperCase());
s +=aa[i]+" ";
}
System.out.println(s);
}
}
运行效果
![运行效果](https://i-blog.csdnimg.cn/blog_migrate/72ca17c21ae49c04bca7484924cbd2ce.png)