You are currently viewing Capitalizing the First Letter of Each Word in a Java String

Capitalizing the First Letter of Each Word in a Java String

Have you ever wanted to make the first letter of each word in a sentence or phrase capitalized? Maybe you’re working on a program that requires proper capitalization for titles, names, or sentences. In Java, capitalizing the first letter of each word in a string is a common task, and fortunately, there are several ways to achieve it efficiently.

In this article, we’ll explore different methods to capitalize the first letter of each word in a Java string. By the end, you’ll have a solid understanding of how to accomplish this task in your Java projects.

Using StringBuilder and StringTokenizer

One of the simplest ways to capitalize the first letter of each word in a Java string is by using the StringBuilder and StringTokenizer classes. This involves splitting the string into tokens, capitalizing the first letter of each token, and then rejoining them back together.

import java.util.StringTokenizer;

public class CapitalizeWords {
	
    public static String capitalizeFirstLetter(String str) {

        StringBuilder result = new StringBuilder();
        StringTokenizer tokenizer = new StringTokenizer(str);
		
        while (tokenizer.hasMoreTokens()) {
            String word = tokenizer.nextToken();
            result.append(Character.toUpperCase(word.charAt(0)))
                    .append(word.substring(1))
                    .append(" ");
        }

        return result.toString().trim();

    }

    public static void main(String[] args) {
        String sentence = "the java programming language";
        System.out.println(capitalizeFirstLetter(sentence));
    }
}

Using Apache Commons Text Library

If you prefer to use a library, Apache Commons Text provides a convenient method for capitalizing the first letter of each word in a string. Before continuing, you need to download the library. Apache Commons Text can be downloaded here, along with Apache Commons Lang. Note that you need to download both and add them to your path.

import org.apache.commons.text.WordUtils;

public class CapitalizeWords {

    public static void main(String[] args) {

        String sentence = "the java programming language";

        String capitalized = WordUtils.capitalizeFully(sentence);

        System.out.println(capitalized);

    }
}

Conclusion

Capitalizing the first letter of each word in a Java string is a common requirement in many applications. In this article, we explored various methods to achieve this task, ranging from a simple manual approach to using libraries like Apache Commons Text.

Whether you prefer to write your own logic using basic string manipulation or leverage existing libraries, Java offers multiple options to accomplish this task efficiently. Depending on the complexity of your project and your specific requirements, you can choose the method that best suits your needs. For more content, please subscribe to our newsletter.

Leave a Reply