如何删除文本文件中的文本

编程入门 行业动态 更新时间:2024-10-28 14:24:35
本文介绍了如何删除文本文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试创建代码以删除特定行,其中是基于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

更多推荐

如何删除文本文件中的文本

本文发布于:2023-11-17 17:00:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!