小编典典

查找两个已知值之间的字符串

c#

我需要能够提取2个标签之间的字符串,例如:“”中的“ 00002
morenonxmldata<tag1>0002</tag1>morenonxmldata

我正在使用C#和.NET 3.5。


阅读 194

收藏
2020-05-19

共1个答案

小编典典

无需正则表达式的解决方案:

string ExtractString(string s, string tag) {
     // You should check for errors in real-world code, omitted for brevity
     var startTag = "<" + tag + ">";
     int startIndex = s.IndexOf(startTag) + startTag.Length;
     int endIndex = s.IndexOf("</" + tag + ">", startIndex);
     return s.Substring(startIndex, endIndex - startIndex);
}
2020-05-19