views:

178

answers:

3

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.

+2  A: 

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

jitter
i agree. seems to be a roll your own situation.
Berek Bryan
A: 

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.

Rakesh Juyal
+2  A: 

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);
    }
}
JG
Thanks JG!!!!!!!!!
Eric Labashosky