How to make every first letter of word uppercase in a java string?

For many Strings we need to make every first letter or first character of a String capital or upper case. Lets see how we can capitalize the String first letter in java.

There are two ways to do it one is you can use WordUtil utility from the thirdparty library apache common lang package.
We can found it at


We can use WordUtils.capitalize(Str) and WordUtils.capitalizeFully(Str) methods, capitalize method capitalizes only first character of the word and do nothing for rest of the characters and in case of capitalizeFully not only it capitalizes first letter of the word but also makes other characters in lower case lets see how to get it.

//First we need to import the WordUtils class

import org.apache.commons.lang.WordUtils;

public class CapitalizeFirstLetterInString {
public static void main(String[] args) {
// only the first letter of each word is capitalized.
String wordStr = WordUtils.capitalize("this is first WORD capital test.");
//Capitalize method capitalizes only first character of a String
System.out.println("wordStr= " + wordStr);

wordStr = WordUtils.capitalizeFully("this is first WORD capital test.");
// This method capitalizes first character of a String and make rest of the characters lowercase
System.out.println("wordStr = " + wordStr );

Output :
This Is First WORD Capital Test.
This Is First Word Capital Test.

Second method is to write your own method :

We can use Character class and String methods to achieve desired result as shown in code below , I have used main method but you can create your own method and use it to capitalize first letter of a word.

public class FirstLetterCapital {

public static void main(String[] args){
String test = "this is first letter capital test";
String[] testSplit = test.split( " " );
StringBuffer sb = new StringBuffer();
for (int i =0 ; i < testSplit.length ; i ++){
String capitalVer = capitalizeFirstLetter(testSplit[i]);
if( i != 0 ){
sb.append(" ");
}catch(Exception es){


public static String capitalizeFirstLetter(final String string)
if (string == null || string.equals("") )
throw new NullPointerException("no string ");

return Character.toUpperCase(string.charAt(0)) + string.substring(1);



This Is First Letter Capital Test
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment