如何删除文本文件中的文本 [英] How can I remove text in text file

查看:132
本文介绍了如何删除文本文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建代码以删除特定行,其中是基于texbox1的文本文件中的文本。



我有这段代码但不是函数,程序返回我错误的sb.remove(线)



你能帮助我吗?



什么我试过了:



Hi, I try to create code for remove specific line where is text in text file based on texbox1.

I have this code but not function, program return me error at sb.remove(line)

Can you help me?

What I have tried:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

      Dim sb As New System.Text.StringBuilder
      For Each line In IO.File.ReadLines("C:\_Montix a.s. - cloud\iMontix\aaa.txt")
          If (line.StartsWith(TextBox1.Text)) Then
              sb.Remove(line)
          End If
      Next






  End Sub

推荐答案

你没有在你的 StringBuilder 对象,因此无需删除任何内容。即使你这样做,该对象也不会在别处使用,所以它没有用处。你的逻辑应该是错误的方式:

You do not put any text in your StringBuilder object, so there is nothing to remove. And even if you did, the object is not used elsewhere so it serves no purpose. Your logic is the wrong way round it should be:
Dim sb As New System.Text.StringBuilder
For Each line In IO.File.ReadLines("C:\_Montix a.s. - cloud\iMontix\aaa.txt")
    If (Not line.StartsWith(TextBox1.Text)) Then
        sb.Append(line) ' add lines that are not in the textbox
    End If
Next
' store the resultant string somewhere


这篇关于如何删除文本文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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