Is there any algorithm in c# to singularize - pluralize a word?

前端 未结 11 1838
臣服心动
臣服心动 2020-12-07 10:55

Is there any algorithm in c# to singularize - pluralize a word (in english) or does exist a .net library to do this (may be also in different languages)?

11条回答
  •  佛祖请我去吃肉
    2020-12-07 11:42

    I cheated in Java - I wanted to be able to produce a correct string for "There were n something(s)", so I wrote the foll. little overloaded utility method:

    static public String pluralize(int val, String sng) {
        return pluralize(val,sng,(sng+"s"));
        }
    
    static public String pluralize(int val, String sng, String plu) {
        return (val+" "+(val==1 ? sng : plu)); 
        }
    

    invoked like so

    System.out.println("There were "+pluralize(count,"something"));
    System.out.println("You have broken "+pluralize(count,"knife","knives"));
    

提交回复
热议问题