【问题标题】:Spliting String into lines and check current lines along side with next line with .net将字符串拆分为行并使用.net检查当前行和下一行
【发布时间】:2020-07-23 06:37:11
【问题描述】:

在使用 '\n' 将字符串拆分为行之后,当您已经在检查当前行时,如何获得下一行中的内容?现在这就是我在 .net 中使用的内容:

 WebClient Downloadurl = new WebClient();
            byte[] myDataBuffer = Downloadurl.DownloadData(url);
            string download = Encoding.ASCII.GetString(myDataBuffer);
            String newfile = download.Replace("<br>", "\n");

            String path = @"E:\BI\projet\dotnet2.csv";
            try
            {
                 Create the file, or overwrite if the file exists.
                using (FileStream fs = File.Create(path))
                {
                    byte[] info = new UTF8Encoding(true).GetBytes(newfile);
                    fs.Write(info,0,info.Length);
                }

【问题讨论】:

    标签: .net string file csv


    【解决方案1】:

    我找到了像下面这样的解决方案

                StreamReader st = new StreamReader(path);
            while (!st.EndOfStream)
            {
                var slpits = st.ReadLine();
                String type = slpits.Substring(0,4);
    
                if (type =="TICN" )
                {
                     ticketid = slpits.Substring(5, 21);
                }
    
                if (type =="SCON")
                {
                    slpits =  slpits.Insert(5, ticketid);
                }
    

    【讨论】:

      【解决方案2】:
      string download = null;
      using (var Downloadurl = new WebClient())
      {
          download = Downloadurl.DownloadString(url).Replace("<br>", "\n");
          // you may want to do something with `<p>`, too,
          // and you should do it in a way that's not case sensitive
      }
      
      string path = @"E:\BI\projet\dotnet2.csv";
      using (var reader = new StringReader(download))
      using (var writer = new StreamWriter(path, false, Encoding.UTF8))
      {
          string line = null;
          while ( (line = reader.ReadLine()) != null)
          {
              // Do something with each line!
              // Note, this doesn't change the original string
      
              writer.WriteLine(line);
          }
      }
      

      【讨论】:

      • 您好,谢谢,但我的问题是当我阅读 while 部分中的每一行时,如何获得下一行?这意味着如果现在我在第 1 行,我如何指向第 2 行并在需要时对其进行修改?
      • 这就是ReadLine() 调用的作用。这将得到所有的行。
      • 是的,如果我错了,我知道是正确的,readline 会读取您所在的当前行,但它无法知道当前行 im askign 如果有任何功能可以执行如下所示:while ( (line = reader.ReadLine()) != null) { if (line.substring(0,4) = "ticp") { line.next = line.next+ "id" } writer.WriteLine(line); }
      • @OUSSAMABEYGAHAR 你可以通过为每一行设置和检查一个标志来做到这一点。所以如果子字符串是ticp,你设置标志。如果设置了标志,则附加 id 值。
      • 非常感谢您抽出宝贵的时间,我真的很欣赏它,我是这个开发环境的新手,所以我可能会被吓到,它不是那么简单,我必须检查一下是否有勾号然后我从中恢复一个 id 然后我去检查下一行是否是 scon 然后我添加我恢复的 id 直到我再次找到我可以恢复的刻度
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 2023-02-08
      • 2021-11-28
      相关资源
      最近更新 更多