4

Possible Duplicate:
Capitalize First Char of Each Word in a String Java

I have a string "abc def ghi" , I want to make it as "Abc Def Ghi".Similarly I also have a string like "abc def" and I want to make it "Abc Def".In java how is it possible?

Community
  • 1
  • 1
Suvonkar
  • 2,440
  • 12
  • 34
  • 44

3 Answers3

2

You can make use of the capitalize method from the WordUtils class of ACL.

codaddict
  • 445,704
  • 82
  • 492
  • 529
2
System.out.println(
  org.apache.commons.lang.WordUtils.capitalize(
    "yellow fox jumped over brown foot"
  )
);

output:

Yellow Fox Jumped Over Brown Foot
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
0

This may not be the most effective, but may give you a starting point:

public String capitalize(String original) {
        String[] parts = original.split(" ");

        StringBuffer result = new StringBuffer();

        for (String part : parts) {
            String firstChar = part.substring(0, 1).toUpperCase();
            result.append(firstChar + part.substring(1));
            result.append(" ");
        }

        String capitalized = result.toString();

        return capitalized.substring(0, capitalized.length()-1);
}
Sergii Pozharov
  • 17,366
  • 4
  • 29
  • 30