編寫一個 java 程式在字串中對每個單詞大小寫 tOGGLE?


您可以透過使用 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

更新日期:26-2-2020

761 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.