Java如何将字符串中的每个单词大写?
此示例说明如何大写字符串。我们使用WordUtilsApache提供的类中的方法commons-text;。我们可以使用WordUtils.capitalize(str)或WordUtils.capitalizeFully(str)。
让我们看下面的例子:
package org.nhooo.example.commons.text;
import org.apache.commons.text.WordUtils;
public class WordCapitalize {
public static void main(String[] args) {
//将字符串中所有用空格分隔的单词大写,
//每个单词只有第一个字母大写。
String str = WordUtils.capitalize(
"The quick brown fox JUMPS OVER the lazy dog.");
System.out.println("str = " + str);
//将字符串中所有由空格分隔的单词大写
//其余字符串为小写。
str = WordUtils.capitalizeFully(
"The quick brown fox JUMPS OVER the lazy dog.");
System.out.println("str = " + str);
}
}这是程序的结果:
str = The Quick Brown Fox JUMPS OVER The Lazy Dog. str = The Quick Brown Fox Jumps Over The Lazy Dog.
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-text/1.7/commons-text-1.7.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.7</version>
</dependency>热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短