0

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.

João Silva
  • 89,303
  • 29
  • 152
  • 158
Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32

4 Answers4

4

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);
    }
}
João Silva
  • 89,303
  • 29
  • 152
  • 158
2

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
  • 53,475
  • 11
  • 111
  • 124
0

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

hd1
  • 33,938
  • 5
  • 80
  • 91
0

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
  • 35,919
  • 68
  • 173
  • 214