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 character of each word into uppercase 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()',它接受一个小写字符并返回相应的大写字符。
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
在下面的示例中,我们将使用用户定义的方法来执行相同的任务和逻辑。
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中文网其他相关文章!