开发者

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

There are many ways to Title Case in java.

But how do you preven开发者_开发百科t 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.


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);
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜