public static void Main(string[] args) { Console.WriteLine("原字符串:abcd提取efgh"); Console.WriteLine("截取的字符串:" + SearchStr("abcd提取efgh", "abcd", "efgh")); Console.ReadKey(); } /// <summary> /// 搜索字符串 /// </summary> /// <param name="s1">目标字符串</param> /// <param name="s2">之前字符串</param> /// <param name="s3">之后字符串</param> /// <returns>获取两个字符串中间的字符串</returns> public static string SearchStr(string s1, string s2, string s3) { int n1, n2; //定义变量 n1 = s1.IndexOf(s2, 0) + s2.Length; //开始位置 n2 = s1.IndexOf(s3, n1); //结束位置 return s1.Substring(n1, n2 - n1); //取搜索的条数,用结束的位置-开始的位置,并返回值 }