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 StringInput String#
simply easy learning tutorialspoint
Output string#
Simply Easy Learning Tutorialspoint要將字串中每個單字的首字母大寫,Java提供了一個內建方法名為'toUpperCase()',它接受一個小寫字元並傳回對應的大寫字元。
Example 1
方法
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
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
以上是Java程式:將字串中每個單字的首字母大寫化的詳細內容。更多資訊請關注PHP中文網其他相關文章!