Question

There are many ways to Title Case in java.

But how do you prevent some of the common abbreviations from being converted. For Example;

System.out.println(makeProper("JAMES A SWEET");
System.out.println(makeProper("123 MAIN ST SW");

James A Sweet
123 Main St Sw

SW should remain uppercase as it is an abbreviation to South West.

Was it helpful?

Solution

You need some sort of dictionary that contains words that should not be capitalized. You could use a HashSet for that. Something along these lines:

Set<String> whiteList = new HashSet<String>();
whiteList.add("SW");
whiteList.add("NW");
// ...
for (String word : phrase.split()) {
    if (!whiteList.contains(word)) {
        makeProper(word);
    }
}

OTHER TIPS

There is no standard way to do such a thing.

You could come up with your own method and a set of abbreviations you don't want "corrected" and exclude does from the transformation

The only way i could see is:

1. split your input text into words  
2. iterate through words  
      2.a  if word is a abbreviation then 
           2.a.1   do nothing or convert to uppercase, whatever is required  
    2.b  otherwise  
           2.b.1   call [makeProper][1] or [capitalize][2]  

makeProper or capitalize
obviously you have to create a method which will decide if the word is an abbreviation or not :) how about keeping all know abbreviation in a file, and reading them.

Apache Commons-lang contains WordUtils.capitalizeFully which I've used to capitalise strings without problem. Hope it saves you time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top