本文介绍了在两个字符串之间拆分字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的记事本文件
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 ?
推荐答案
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
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)
<div class="info"></div>
标签。然后,您可以从每个搜索结束
tags. From each one you can then search for the closing
并将中间内容存储在字符串数组中。
或者,由于这似乎是XML,请使用 [],使用其中一种加载方法( [ ], [], [],...)并检查其 []。
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[^].
这篇关于在两个字符串之间拆分字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!