小编Gio*_*i19的帖子

C# 逐行读取文本文件并编辑特定行

我想逐行读取文本文件并编辑特定行。因此,我将文本文件放入字符串变量中,例如:

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)

我想清除第三个读取行并将另一个字符串放入该行并将整个保存stringtextFile.

我怎样才能做到这一点?

c# string stringwriter stringreader

6
推荐指数
1
解决办法
3万
查看次数

Jsoup仅在元素存在的情况下获取

我正在寻找一种可能只在它存在的情况下获取元素的可能性.否则我会收到错误,因为它不存在.

以下是我的案例:

  • 带有"tr"标签的表(例如3).
  • 代码正在调查每个"tr"以搜索特定数据.如果不存在,则查找下一个"tr"元素.这里,如果没有更多"tr"元素,则会发生错误.

是)我有的:

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"元素,我需要一个构造来检查.

java element jsoup

4
推荐指数
1
解决办法
3860
查看次数

标签 统计

c# ×1

element ×1

java ×1

jsoup ×1

string ×1

stringreader ×1

stringwriter ×1