我想逐行读取文本文件并编辑特定行。因此,我将文本文件放入字符串变量中,例如:
string textFile = File.ReadAllText(filename);
我的文本文件如下:
Line A
Line B
Line C
Line abc
Line 1
Line 2
Line 3
Run Code Online (Sandbox Code Playgroud)
我有一个特定的字符串(=“abc”),我想在此文本文件中搜索它。因此,我正在阅读这些行,直到找到字符串并在找到字符串之后转到第三行(“第 3 行”-> 该行始终不同):
string line = "";
string stringToSearch = "abc";
using (StringReader reader = new StringReader(textFile))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(stringToSearch))
{
line = reader.ReadLine();
line = reader.ReadLine();
line = reader.ReadLine();
//line should be cleared and put another string to this line.
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想清除第三个读取行并将另一个字符串放入该行并将整个保存string到textFile.
我怎样才能做到这一点?
我正在寻找一种可能只在它存在的情况下获取元素的可能性.否则我会收到错误,因为它不存在.
以下是我的案例:
是)我有的:
Element e = doc.getElementsByClass("table table-striped table-hover nofooter-").first();
Element tbody = e.select("tbody").first();
int j = 0;
while(tbody != null){
Element tr = tbody.select("tr").get(j); //Look for the next "tr" --> HERE: error, because there is no more "tr", if string "A" not found in all existing "tr"s.
if(tr != null){
if(string == "A"){
//Do something
}
j = j+1; //Increment for looking for the next "tr"
}
}
Run Code Online (Sandbox Code Playgroud)
如果存在"next""tr"元素,我需要一个构造来检查.