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.
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程序:将字符串中每个单词的首字母大写化的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!