public static class StringUtil { /// <summary> /// 單詞變成單數形式 /// </summary> /// <param name="word"></param> /// <returns></returns> public static string ToSingular(string word) { Regex plural1 = new Regex("(?<keep>[^aeiou])ies$"); Regex plural2 = new Regex("(?<keep>[aeiou]y)s$"); Regex plural3 = new Regex("(?<keep>[sxzh])es$"); Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$"); if (plural1.IsMatch(word)) return plural1.Replace(word, "${keep}y"); else if (plural2.IsMatch(word)) return plural2.Replace(word, "${keep}"); else if (plural3.IsMatch(word)) return plural3.Replace(word, "${keep}"); else if (plural4.IsMatch(word)) return plural4.Replace(word, "${keep}"); return word; } /// <summary> /// 單詞變成復數形式 /// </summary> /// <param name="word"></param> /// <returns></returns> public static string ToPlural(string word) { Regex plural1 = new Regex("(?<keep>[^aeiou])y$"); Regex plural2 = new Regex("(?<keep>[aeiou]y)$"); Regex plural3 = new Regex("(?<keep>[sxzh])$"); Regex plural4 = new Regex("(?<keep>[^sxzhy])$"); if (plural1.IsMatch(word)) return plural1.Replace(word, "${keep}ies"); else if (plural2.IsMatch(word)) return plural2.Replace(word, "${keep}s"); else if (plural3.IsMatch(word)) return plural3.Replace(word, "${keep}es"); else if (plural4.IsMatch(word)) return plural4.Replace(word, "${keep}s"); return word; } }
一般加s.比如run-runs.以y結尾該y為i加es.比如fly-flies.但如果y前面是元音字母即aeiou.如則直接加s,還有以f或fe結尾的.加.如.knife-knives
單詞在哪?