搜尋
首頁Javajava教程Java程式:將字串中每個單字的首字母大寫化

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

Aug 20, 2023 pm 03:45 PM
字串java編程首字母大寫

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。如有侵權,請聯絡admin@php.cn刪除
如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?Mar 17, 2025 pm 05:46 PM

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?Mar 17, 2025 pm 05:44 PM

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?Mar 17, 2025 pm 05:43 PM

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Mar 17, 2025 pm 05:35 PM

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具