导航

yaosansi's Blog

当你背向太阳的时候,你只会看到自己的阴影!能力是有限的,努力无限的!

« 读取Sql Server2000,Sql Server2005中的表结构的相关信息N 层架构应用程序中的数据检索和 CUD 操作 (LINQ to SQL) »

C#英文单词单数复数相互转换(string Plural Singular)

  • 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
  • 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
  • QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
  • QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
  • QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com
 

转载请标明出处: http://www.yaosansi.com

原文:http://www.yaosansi.com/post/1378.html

 
 
单数转复数
 
1.字符串替换
      public static String toPlural(String name)
       {
           String plural;
           if (name != null && name.Length> 0)
           {
               char ch = name[name.Length - 1];
               StringBuilder pluralBuilder = new StringBuilder(name);
               if (ch == 's' || ch == 'x')
               {
                   pluralBuilder.Append("es");
               }
               else if (ch == 'y')
               {
                   pluralBuilder.Remove(pluralBuilder.Length - 1,1);
                   pluralBuilder.Append("ies");
               }
               else
               {
                   pluralBuilder.Append("s");
               }
               plural = pluralBuilder.ToString();
           }
           else
           {
               plural = name;
           }
           return plural;
       }
 
2.正则表达式匹配
 
       public string getPluralForWord(string word)
       {
           string plural = null;
           Regex ES = new Regex("^.*(sh|ss|ch|o|i)$");
           Regex ICES = new Regex("^.*(ex|ix)$");
           Regex NOT_VOWEL_Y = new Regex("^.*[^aeiou]y$");
 
           if (!string.IsNullOrEmpty(word))
           {
               plural = word + "s";
               if (ES.Match(word).Success)
               {
                   plural = word + "es";
               }
               else if (NOT_VOWEL_Y.Match(word).Success)
               {
                   String stripY = word.Substring(0, word.Length - 1);
                   plural = stripY + "ies";
               }
               else if (ICES.Match(word).Success)
               {
                   String strip_X = word.Substring(0, word.Length - 2);
                   plural = strip_X + "ices";
               }
           }
 
           return plural;
       }
 
 
复数转单数
       /// <summary>
       ///  Convert the input word from plural to singular
       /// </summary>
       /// <param name="plural"></param>
       /// <returns></returns>
       private string PluralToSingular(string plural)
       {
           // convert to lowercase for easier comparison
           String lower = plural.ToLower();
           string res = string.Empty;
           // rule out a few exceptions
           if (lower == "feet")
           {
               res = "Foot";
           }
           else if (lower == "geese")
           {
               res = "Goose";
           }
           else if (lower == "men")
           {
               res = "Man";
           }
           else if (lower == "women")
           {
               res = "Woman";
           }
           else if (lower == "criteria")
           {
               res = "Criterion";
           }
           // plural uses "ies" if word ends with "y" preceeded by a non-vowel
           else if (lower.EndsWith("ies") && "aeiou".IndexOf(lower.Substring(lower.Length - 4, 1)) < 0)
           {
               res = plural.Substring(0, plural.Length - 3) + "y";
           }
           else
           {
               res = plural.Substring(0, plural.Length - 1);
           }
 
           // the result must preserve the original word//s capitalization
           if (plural == lower)
           {
               return res.ToLower();// it was an all-lowercase word
           }
           else if (plural == plural.ToUpper())
           {
               return res.ToUpper();   // it was an all-uppercase word
           }
           else
           {
               return res;    // return whatever is in "res"
           }
 
       }

原创文章如转载,请注明:转载自http://www.yaosansi.com
订阅本站,阅读更多文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog .Theme from Google黑板报 By Washun

Copyright 2005-2008 yaosansi'site All Rights Reserved.

感谢Denny·G 为本站提供FTP空间
辽ICP备05021434号

Search

  •  

赞助商广告

控制面板

最新评论及回复

最近发表