首頁 > Java > java教程 > 主體

Java程式:將字串中每個單字的首字母大寫化

王林
發布: 2023-08-20 15:45:13
轉載
1257 人瀏覽過

Java程式:將字串中每個單字的首字母大寫化

A string is a class of 'java.lang' package that stores a series of characters. Those characters are actually String-type objects. We must enclose the value of string within double quotes . Generally, we can represent characters in lowercase and uppercase in Java. And, it is also possible to convert lowercase characters into uppercase. This article aims to discuss a Java program to convert the first.

##Java program to Capitalize the first character of each word in a String

Before making a Java program to convert the first lowercase character of a string into uppercase, let's understand the problem statement first with the help of an example −

#實例

Input String#

simply easy learning tutorialspoint
登入後複製

Output string#

Simply Easy Learning Tutorialspoint
登入後複製

要將字串中每個單字的首字母大寫,Java提供了一個內建方法名為'toUpperCase()',它接受一個小寫字元並傳回對應的大寫字元。

Example 1

的中文翻譯為:

範例 1

The following example demonstrates how we can capitalize the first character of each word in a string.

方法

  • First, declare and initialize a String. Then, convert that string into a character array using an in-built method named 'toCharArray()'.

  • 接下來,取一個for循環,它將運行到字元陣列的大小。

  • 在這個for迴圈內部,定義一個if區塊來檢查字元陣列是否包含空格。如果編譯器遇到空格,則將下一個字元轉換為大寫並更新數組。

  • 現在,我們需要將字元陣列轉換回字串。

  • In the end, print the result and exit.

  • #
    public class Capitalize {
       public static void main(String[] args) {
          String myinput = "simply easy learning tutorialspoint";
          // store each character to a char array
          char[] charAray = myinput.toCharArray();
          System.out.println("Before capitalizing: " + myinput);
          // for loop to capitalize first letter 
          for(int i = 0; i < charAray.length; i++) {
             // capitalizing first letter of first word
             charAray[0] = Character.toUpperCase(charAray[0]);
             // loop to check if there is space between two letters
             if(charAray[i] == ' ') {
                // capitalizing first letter of rest of the word
                charAray[i+1] = Character.toUpperCase(charAray[i+1]);
             }
          }
          // converting the character array to the string
          myinput = String.valueOf(charAray);
          // to print the final result
          System.out.println("After capitalizing the first letter: " + myinput);
       }
    }
    
    登入後複製
輸出

Before capitalizing: simply easy learning tutorialspoint
After capitalizing the first letter: Simply Easy Learning Tutorialspoint
登入後複製
登入後複製

Example 2

翻譯成中文為:

範例2

在下面的範例中,我們將使用使用者定義的方法來執行相同的任務和邏輯。

public class Capitalize {
   public static void Capital(String myinput) { // user-defined method
      // store each character to a char array
      char[] charAray = myinput.toCharArray();
      // for loop to capitalize first letter 
      for(int i = 0; i < charAray.length; i++) {
         // capitalizing first letter of first word
         charAray[0] = Character.toUpperCase(charAray[0]);
         // loop to check if there is space between two letters
         if(charAray[i] == ' ') {
            // capitalizing first letter of rest of the word
            charAray[i+1] = Character.toUpperCase(charAray[i+1]);
         }
      }
      // converting the character array to the string
      myinput = String.valueOf(charAray);
      // to print the final result
      System.out.println("After capitalizing the first letter: " + myinput); 
   }
   public static void main(String[] args) {
      String myinput = "simply easy learning tutorialspoint";
      System.out.println("Before capitalizing: " + myinput);
      Capital(myinput); // calling the method to capitalize
   }
}
登入後複製

輸出

Before capitalizing: simply easy learning tutorialspoint
After capitalizing the first letter: Simply Easy Learning Tutorialspoint
登入後複製
登入後複製
結論

在本文中,我們討論了兩種方法來將字串中每個單字的首字母大寫。但是,這兩種方法共同的一點是內建方法 'toUpperCase()',它將小寫字元轉換為大寫字元。

以上是Java程式:將字串中每個單字的首字母大寫化的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板