代码之家  ›  专栏  ›  技术社区  ›  Eric Labashosky

在Java中,如何在保留一些常用缩略语的同时给字符串加上大小写标题

  •  0
  • Eric Labashosky  · 技术社区  · 16 年前

    有很多方法可以解决这个问题 Title Case in java .

    System.out.println(makeProper("JAMES A SWEET");
    System.out.println(makeProper("123 MAIN ST SW");
    
    James A Sweet
    123 Main St Sw
    

    SW应保持大写,因为它是西南的缩写。

    4 回复  |  直到 16 年前
        1
  •  4
  •   João Silva    16 年前

    你需要某种字典,其中包含不应该大写的单词。你可以用一个 HashSet 为此。大致如下:

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

    您可以提出自己的方法和一组不希望“更正”的缩写,并将DO从转换中排除

        3
  •  0
  •   Rakesh Juyal    16 年前

    我唯一能看到的是:

    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 capitalize
    显然,你必须创建一个方法来决定这个词是否是缩写词

        4
  •  0
  •   hd1 Madan Sapkota    14 年前

    Apache Commons-lang 包含WordUtils.capitalizely,我用它来大写字符串,没有问题。希望能节省你的时间。

    推荐文章