/// <summary>
/// 截取中间字符
/// </summary>
/// <param name="text">全字符串</param>
/// <param name="start">开始字符串 </param>
/// <param name="end">结束字符串 </param>
/// <returns></returns>
public static string Substring(string text,string start,string end)
{
//int IndexofA = text.IndexOf(start);
//int IndexofB = text.IndexOf(end);
//string NameText = text.Substring(IndexofA + start.Length, IndexofB - IndexofA + 2 - end.Length);
Regex rg = new Regex("(?<=(" + start + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
string NameText = rg.Match(text).Value;
return NameText;
}
c# 怎样截取两特定字符之间的字符串
最新推荐文章于 2024-01-19 09:32:54 发布
本文介绍了一种使用正则表达式从源字符串中截取出位于两个特定字符串之间的子串的方法。这种方法避免了直接使用索引进行截取,提高了代码的健壮性和灵活性。
1511

被折叠的 条评论
为什么被折叠?



