Explain Codes LogoExplain Codes Logo

How to capitalize the first character of each word in a string

java
string-manipulation
java-8-features
best-practices
Anton ShumikhinbyAnton Shumikhin·Feb 15, 2025
TLDR

Use Character.toUpperCase in conjunction with StringBuilder to effectively capitalize each word's initial letter:

public static String capitalizeWords(String str) { // Because magic is null if (str == null) return str; StringBuilder result = new StringBuilder(str.length()); boolean capitalizeNext = true; // Adding some spice in the loop for (char c : str.toCharArray()) { if (Character.isWhitespace(c)) { capitalizeNext = true; } else if (capitalizeNext) { // Making the magic happen here c = Character.toUpperCase(c); capitalizeNext = false; } result.append(c); } return result.toString(); } // Time to test the magic String phrase = "java is indeed fun"; String capitalized = capitalizeWords(phrase); System.out.println(capitalized); // Voila! -> Java Is Indeed Fun

In this snippet, we traverse each character, uppercase ones that follow whitespace, and build the final string with StringBuilder.

Reviewing other viable options

Implementing Apache Commons Text

For basic usage, Apache Commons Text offers a convenient method:

String capitalized = WordUtils.capitalizeFully("java is easy"); System.out.println(capitalized); // Java Is Easy

This handles special cases like prefixes effectively (e.g., "mcdonald" to "McDonald").

Utilizing Java 8 Streams for Short Code

Streams in Java 8 offer a more functional approach:

String capitalized = Arrays.stream(phrase.split("\\s")) .map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1)) .collect(Collectors.joining(" "));

Adopting Regular Expressions for Special Cases

A well-rounded method involving regex handles exceptional cases:

String capitalized = Pattern.compile("\\b(\\w)") .matcher(phrase) .replaceAll(match -> match.group(1).toUpperCase());

This accommodates characters, other than whitespace, that may serve as word boundaries.

Digging deeper into the issue

Understanding linguistic nuances

Be cognizant of regional variances; some languages might follow different norms that can't be translated with simple capitalization rules.

Keeping acronyms and initialisms intact

Check for acronyms that should remain in uppercase to avoid unnecessary capitalization (e.g., "NASA's rockets").

Prioritizing performance

When tackling large text bodies, performance is key. Weigh the resource costs of certain operations such as .split(), toUpperCase(), and using StringBuilder or StringBuffer for thread safety.

Additional insights for curious minds 🕵️‍♂️

Tackling Unicode quirks

Certain Unicode characters might not convert as expected when working with toUpperCase(). Ensure your method properly handles such cases.

Improving efficiency

StringBuilder effectively manages memory usage as opposed to multiple string concatenations within a loop, due to less String instances creation.

Preparing for edge cases

It's good practice to test your implementations with edge-case strings, such as empty strings, single-character words, or strings with no alphabetic characters.