寫一個 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP