编写一个Java程序来了解字符串中的每个单词?
您可以使用toUpperCase()和toLowerCase()方法更改单词字母的大小写。
使用 split()方法拆分字符串中的每个单词,将每个单词的首字母更改为小写,其余字母更改为大写。
示例
public class Sample{
public static void main(String args[]){
String sample = "Hello How are you";
String[] words = sample.split(" ");
String result = "";
for(String word:words){
String firstSub = word.substring(0, 1);
String secondSub = word.substring(1);
result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";
}
System.out.println(result);
}
}输出结果
hELLO hOW aRE yOU