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 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程序:将字符串中每个单词的首字母大写化的详细内容。更多信息请关注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内置函数trim()PHP内置函数trim()可以去除字符串两端的空格(包括空格、制表符、换行符等),非常方便且简单易用

PHP字符串匹配技巧:避免模糊包含表达式在PHP开发中,字符串匹配是一个常见的任务,通常用于查找特定的文本内容或验证输入的格式。然而,有时候我们需要避免使用模糊的包含表达式来确保匹配的准确性。本文将介绍一些在PHP中进行字符串匹配时避免模糊包含表达式的技巧,并提供具体的代码示例。使用preg_match()函数进行精确匹配在PHP中,可以使用preg_mat
