In Java how do you Title Case a String while preserving some common abbreviations

2.9k views Asked by At

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.

4

There are 4 answers

0
João Silva On BEST ANSWER

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);
    }
}
1
jitter On

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

0
Rakesh Juyal On

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.

0
hd1 On

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