查找 2 个已知值之间的字符串 [英] Find a string between 2 known values
本文介绍了查找 2 个已知值之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要能够提取 2 个标签之间的字符串,例如:00002" from "morenonxmldata
"
I need to be able to extract a string between 2 tags for example: "00002" from "morenonxmldata<tag1>0002</tag1>morenonxmldata
"
我使用的是 C# 和 .NET 3.5.
I am using C# and .NET 3.5.
推荐答案
无需正则表达式的解决方案:
Solution without need of regular expression:
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);
}
这篇关于查找 2 个已知值之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文