本文介绍了如何删除文本文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试创建代码以删除特定行,其中是基于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
更多推荐
如何删除文本文件中的文本
发布评论