查找两个已知值之间的字符串 [英] Find a string between 2 known values

查看:67
本文介绍了查找两个已知值之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

这篇关于查找两个已知值之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆