在两个字符串之间拆分字符 [英] split string between two string

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

问题描述

我有一个这样的记事本文件



i have one notepad file like this

Some data..
<div class="info">
                                    208,Kunj Enclave, Opp Krishna Bunglows,Near Ambe School,Beside Vadsar Bridge
                                 </div>
<div class="info">
                                    208,Kunj Enclave, Opp Krishna Bunglows,Near Ambe School,Beside Vadsar Bridge
                                 </div>
<div class="info">
                                    208,Kunj Enclave, Opp Krishna Bunglows,Near Ambe School,Beside Vadsar Bridge
                                 </div>


Some data..





现在我想要一些字符串[]格式的数据在



now i want some data in string[] format in between

<div class="info">

</div>

标记我可以为它做些什么?

tag so what can i do for it ?

推荐答案

试试这个:

Try this:
FileStream fs = new FileStream(@"C:\Users\amit\Desktop\Test.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadToEnd();
string [] separator = {"<div class=\"info\">", "</div>"};//Store the separator in string array
string[] newStrings = str.Split(separator, StringSplitOptions.None);//Split the string using that seperator. You can again split these values using comma(,) and get the strings.





希望它有所帮助!

--Amit



Hope it helps!
--Amit


如果您使用的是ASP.NET,并且HTML文件与您的网站路径相关:

If you're using ASP.NET, and the HTML file is relative to your website path:
string html = File.ReadAllText(Server.MapPath("~/virtualPathTo/file.html"));
int start_index = html.IndexOf("<div class="info">") + 18;
int end_index = html.IndexOf("</div>") + 6;
string[] return1 = html.Substring(start_index, end_index - start_index).Split(,);

html = html.Substring(end_index);        //suppress previous div from string

... (do the same for getting the others div informations)


重复使用 System.String。 IndexOf() [ ^ ]或一次性 System.String.IndexOfAny() [ ^ ]您可以获得所有
By repeated use of System.String.IndexOf()[^] or a one-time System.String.IndexOfAny()[^] you can get all the
<div class="info"></div>

标签。然后,您可以从每个搜索结束

tags. From each one you can then search for the closing


并将中间内容存储在字符串数组中。



或者,由于这似乎是XML,请使用系统。 Xml.XmlDocument [ ^ ],使用其中一种加载方法(加载(TextReader) [ ^ ],加载(流) [ ^ ], LoadXml(String) [ ^ ],...)并检查其 ChildNodes [ ^ ]。

and store the in-between stuff in your string array.

Or, since this seems to be XML, use aSystem.Xml.XmlDocument[^], feed it with one of its Load methods (Load(TextReader)[^], Load(Stream)[^], LoadXml(String)[^], ...) and check its ChildNodes[^].


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

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