1、正则表达式
public static string MidStrEx_New(string sourse, string startstr, string endstr) { Regex rg = new Regex("(?<=(" + startstr + "))[.\s\S]*?(?=(" + endstr + "))", RegexOptions.Multiline | RegexOptions.Singleline); return rg.Match(sourse).Value; }
2.利用字符串indexof截取:
public static string MidStrEx(string sourse, string startstr, string endstr) { string result = string.Empty; int startindex, endindex; try { startindex = sourse.IndexOf(startstr); if (startindex == -1) return result; string tmpstr = sourse.Substring(startindex + startstr.Length); endindex = tmpstr.IndexOf(endstr); if (endindex == -1) return result; result = tmpstr.Remove(endindex); } catch (Exception ex) { Log.WriteLog("MidStrEx Err:" + ex.Message); } return result; }