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 StringBefore 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
Example 1
的中文翻譯為:
範例 1The 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 2Before capitalizing: simply easy learning tutorialspoint After capitalizing the first letter: Simply Easy Learning Tutorialspoint
翻譯成中文為:
範例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()',它將小寫字元轉換為大寫字元。
Before capitalizing: simply easy learning tutorialspoint After capitalizing the first letter: Simply Easy Learning Tutorialspoint
以上是Java程式:將字串中每個單字的首字母大寫化的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP中int型別轉字串的方法詳解在PHP開發中,常會遇到將int型別轉換為字串型別的需求。這種轉換可以透過多種方式實現,本文將詳細介紹幾種常用的方法,並附帶具體的程式碼範例來幫助讀者更好地理解。一、使用PHP內建函數strval()PHP提供了一個內建函數strval(),可以將不同類型的變數轉換為字串類型。當我們需要將int型別轉換為字串型別時,

1.先開啟pycharm,進入到pycharm首頁。 2.然後新建python腳本,右鍵--點選new--點選pythonfile。 3.輸入一段字串,代碼:s="-"。 4.接著需要把字串裡面的符號重複20次,代碼:s1=s*20。5、輸入列印輸出代碼,代碼:print(s1)。 6.最後運行腳本,在最底部會看到我們的回傳值:-就重複了20次。

標題:Golang中判斷字串是否以指定字元結尾的方法在Go語言中,有時候我們需要判斷一個字串是否以特定的字元結尾,這在處理字串時十分常見。本文將介紹如何使用Go語言來實現這項功能,同時提供程式碼範例供大家參考。首先,讓我們來看看Golang中如何判斷一個字串是否以指定字元結尾的方法。 Golang中的字串可以透過索引來取得其中的字符,而字串的長度可

Golang中如何檢查字串是否以特定字元開頭?在使用Golang程式設計時,經常會遇到需要檢查一個字串是否以特定字元開頭的情況。針對這項需求,我們可以使用Golang中的strings套件所提供的函數來實現。接下來將詳細介紹如何使用Golang檢查字串是否以特定字元開頭,並附上具體的程式碼範例。在Golang中,我們可以使用strings套件中的HasPrefix

解決PHP中16進位轉字串出現中文亂碼的方法在PHP程式設計中,有時候我們會遇到需要將16進位表示的字串轉換為正常的中文字元的情況。然而,在進行這個轉換的過程中,有時會遇到中文亂碼的問題。這篇文章將為您提供解決PHP中16進位轉字串出現中文亂碼的方法,並給出具體的程式碼範例。使用hex2bin()函數進行16進位轉換PHP內建的hex2bin()函數可以將1

Go語言是一種強大且靈活的程式語言,它提供了豐富的字串處理功能,包括字串截取。在Go語言中,我們可以使用切片(slice)來截取字串。接下來,將詳細介紹如何在Go語言中截取字串,並附上具體的程式碼範例。一、使用切片截取字串在Go語言中,可以使用切片表達式來截取字串的一部分。切片表達式的語法如下:slice:=str[start:end]其中,s

PHP字串比對技巧:避免模糊包含表達式在PHP開發中,字串比對是常見的任務,通常用於尋找特定的文字內容或驗證輸入的格式。然而,有時候我們需要避免使用模糊的包含表達式來確保匹配的準確性。本文將介紹一些在PHP中進行字串匹配時避免模糊包含表達式的技巧,並提供具體的程式碼範例。使用preg_match()函數進行精確比對在PHP中,可以使用preg_mat

PHP字串操作:有效去除空格的實用方法在PHP開發中,經常會遇到需要對字串進行去除空格操作的情況。去除空格可以讓字串更加整潔,方便後續的資料處理和顯示。本文將介紹幾種有效的去除空格的實用方法,並附上具體的程式碼範例。方法一:使用PHP內建函數trim()PHP內建函數trim()可以移除字串兩端的空格(包括空格、製表符、換行符等),非常方便且簡單易用
