Clever way to append 's' for plural form in .Net (syntactic sugar)

前端 未结 14 1631
抹茶落季
抹茶落季 2021-01-30 06:53

I want to be able to type something like:

Console.WriteLine(\"You have {0:life/lives} left.\", player.Lives);

instead of

Consol         


        
14条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 07:17

    using @Darin Dimitrov solution, I would create an extention for string ....

    public static Extentions
    {
        public static string Pluralize(this string str,int n)
        {
            if ( n != 1 )
                return PluralizationService.CreateService(new CultureInfo("en-US"))
                .Pluralize(str);
            return str;
        }
    }
    
    string.format("you have {0} {1} remaining",liveCount,"life".Pluralize());
    

提交回复
热议问题