Rentetan ialah kelas pakej 'java.lang' yang menyimpan satu siri aksara tersebut sebenarnya adalah objek jenis rentetan. Kita mesti menyertakan nilai rentetan dalam petikan berganda huruf kecil dan huruf besar dalam Java Dan, ia juga mungkin untuk menukar aksara kecil kepada huruf besar Artikel ini bertujuan untuk membincangkan program Java untuk menukar aksara pertama setiap perkataan kepada huruf besar dalam rentetan.
Atur cara Java untuk Mengubah huruf besar huruf pertama setiap perkataan dalam StringContoh
Rentetan Input
simply easy learning tutorialspoint
Rentetan keluaran
Simply Easy Learning Tutorialspoint
Terjemahan bahasa Cina bagi
Contoh 1Kaedah
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
Atas ialah kandungan terperinci Program Java: Gunakan huruf besar pertama setiap perkataan dalam rentetan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!