- 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
- 本站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 comparisonString lower = plural.ToLower();
string res = string.Empty;
// rule out a few exceptionsif (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-vowelelse 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"
}
}
订阅本站,阅读更多文章