using System; namespace ConsoleApp { class Program { static void Main(string[] args) { string str = "askljd sadsa dsakld sa?dsad"; Console.WriteLine(str.GetWordCount()); Console.ReadKey(); } } /// <summary> /// 扩展方法的写法:静态类-->静态方法 /// </summary> public static class stringExtension { /// <summary> /// 语法 this 对那个类型的扩展 参数 /// </summary> /// <param name="str"></param> /// <returns></returns> public static int GetWordCount( this string str) { return str.Split(new char[] {‘ ‘,‘?‘,‘:‘,‘,‘,‘.‘},StringSplitOptions.RemoveEmptyEntries).Length; } } }