Home > Java > javaTutorial > How Can I Capitalize the First Letter of Each Word in a Java String?

How Can I Capitalize the First Letter of Each Word in a Java String?

Patricia Arquette
Release: 2024-12-25 14:56:16
Original
906 people have browsed it

How Can I Capitalize the First Letter of Each Word in a Java String?

Capitalizing Initial Characters in String Words with Built-in Java Function

In Java, the built-in function WordUtils.capitalize(str) from the Apache Commons Text library can capitalize the first character of each word in a given string while leaving other characters unaffected.

Usage:

To capitalize the first character of each word in a string, simply use the following syntax:

String capitalizedString = WordUtils.capitalize(originalString);
Copy after login

Examples:

Consider the following examples to illustrate the behavior of the WordUtils.capitalize() function:

  • "jon skeet" → "Jon Skeet"
  • "miles o'Brien" → "Miles O'Brien" (Capitalization remains on 'B')
  • "old mcdonald" → "Old Mcdonald"

Note:

If you wish to capitalize the first character of every word, including conjunctions like "and" and "the," you can use the WordUtils.capitalizeFully(..) function instead. This will result in the following output for the given example:

  • "old mcdonald" → "Old McDonald"

The above is the detailed content of How Can I Capitalize the First Letter of Each Word in a Java String?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template