c#截取两个指定字符串中间的字符串写法有很多,记录常⽤的两种:
1、正则表达式
1public static string MidStrEx_New(string sourse, string startstr, string endstr)
2        {
3            Regex rg = new Regex("(?<=(" + startstr + "))[.\\s\\S]*?(?=(" + endstr + "))", RegexOptions.Multiline | RegexOptions.Singleline); 4return rg.Match(sourse).Value;
5        }
2、利⽤字符串indexof截取:
怎么截取列表中的字符串1public static string MidStrEx(string sourse, string startstr, string endstr)
2        {
3string result = string.Empty;
4int startindex, endindex;
5try
6            {
7                startindex = sourse.IndexOf(startstr);
8if (startindex == -1)
9return result;
10string tmpstr = sourse.Substring(startindex + startstr.Length);
11                endindex = tmpstr.IndexOf(endstr);
12if (endindex == -1)
13return result;
14                result = tmpstr.Remove(endindex);
15            }
16catch (Exception ex)
17            {
18                Log.WriteLog("MidStrEx Err:" + ex.Message);
19            }
20return result;
21        }
就效率来说,测试了⼏次,⽅法2⽐⽅法1⼤约快10倍

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。