使用数组从文本文件中删除文本 [英] Deleting text from a text file using an Array

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

问题描述

大家好,我是VB的新手并且正在开展一个项目。 


我想知道如何使用文本文件从文本文件中删除文本阵列。我的代码看起来应该可行,但它没有,你们能给我任何建议吗?

 Imports System.IO 

公共类frmActivities
Dim file_location As String =" ... \ activities.txt"
Dim Searchitem As String
Dim Saveitem As String
Dim Activity As String =""
Dim Activities(1000)As String
Dim Deleteitem As String =
Dim i As Integer = 0
Dim x As String =""


...........



私有子btnDelete_Click(发件人为对象,e为EventArgs )处理btnDelete.Click

Dim sr As New StreamReader(file_location)
Do until sr.Peek = -1

Deleteitem = txtSearchsave.Text
Activity = sr.ReadLine()
活动(i)=活动'创建数组
i = i + 1'增量=增量+ 1 - 这意味着每次循环增量增加1.
循环
如果(Activities.Contains(Deleteitem))则'如果文本文件中的行包含文本框中的数据则...

File.WriteAllText(file_location,File .ReadAllText(file_location).Replace((Deleteitem),""))
End if

End Sub


谢谢。

解决方案


我是想知道如何从中删除文本使用数组的文本文件。我的代码看起来应该可以工作,但它只是没有b



如果你的代码不起作用,你必须描述你期望发生的事情以及实际发生的事情。 似乎代码替换了文件中的文本框字符串,而不是删除它。


当你将复杂的功能串在一起时,你无法正确调试代码。   ; 将读/写行分解为其元素: 将文件中的所有行读入一个数组(这是你已经拥有的循环中的代码),
然后,在另一个循环中,将所有行从数组写入 new 文件
跳过匹配
文本框的行。使用新文件可以比较两个文件,并根据需要重新运行多次。您需要跟踪计数(I)以控制要写入的项目数。


这样可以查看数组数据是什么并确认匹配正在发生。 请注意,您当前的代码执行两种不同的比较 - .Contains数组方法和.Replace方法,无论ReadAlText
返回什么。 你确定他们会返回相同的结果吗?  您只需使用一个比较操作即可避免需要知道代码。


当您使用它时,添加代码以删除原始文件并将新文件重命名为原始名称。


'Deleteitem = txtSearchsave.Text'不应该在循环内 - 它永远不会改变。  您不应该同时在同一文件上使用streamwriter和FileIO - 完成后关闭StreamWriter。  (Deleteitem)
不需要括号,你应该确保WriteAllText中的Append争论是显式的(虽然这变得没有实际意义)。


在数组元素中读取一行:
https://msdn.microsoft .com / zh-cn / library / system.io.streamreader.readline(v = vs.110).aspx


从数组元素写一行:
https://msdn.microsoft.com/en-us/library/7ack4zyt( v = vs.110).aspx





Hi guys, I am fairly new to VB and working on a project. 

I'd like to know how to remove text from a text file using an Array. My code looks like it should work, but it just doesn't, Can you guys give me any suggestions?

Imports System.IO

Public Class frmActivities
    Dim file_location As String = "...\activities.txt"
    Dim Searchitem As String
    Dim Saveitem As String
    Dim Activity As String = ""
    Dim Activities(1000) As String
    Dim Deleteitem As String =
    Dim i As Integer = 0
    Dim x As String = ""


...........



    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

        Dim sr As New StreamReader(file_location)
        Do Until sr.Peek = -1

            Deleteitem = txtSearchsave.Text
            Activity = sr.ReadLine()
            Activities(i) = Activity 'Creates array
            i = i + 1 'increment = increment+1 - this means that each time it loops the increment increases by 1.
        Loop
        If (Activities.Contains(Deleteitem)) Then ' If the line from the text file contains the data from the textbox then...

            File.WriteAllText(file_location, File.ReadAllText(file_location).Replace((Deleteitem), ""))
        End If

    End Sub

Thanks.

解决方案

I'd like to know how to remove text from a text file using an Array. My code looks like it should work, but it just doesn't

If you have code that doesn't work you must describe what you expect to happen and what actually happens.  It appears that the code replaces the text box string in the file, rather than deleting it.

You can't debug code properly when you have complex functions strung together like that.    Break that read/write line down into its elements:  Read all lines form the file into an array (that's the code in the loop you already have), then, in another loop, write all lines from the array to a new file skipping the line that matches the text box. Using a new file will allow you to compare the two files, and re-run as many times as you like. You will need to keep track of the count (I) to control the number of items to write.

This will make it possible to see what the array data is and to confirm that the match is occurring properly.  Note that your current code does two different compares - .Contains method of an array and .Replace method of whatever it is that ReadAlText returns.  Are you quite sure they will return the same result?   You code should avoid the need to know that by using only one compare operation.

When you have it working, add code to delete the original file and rename the new file to the original name.

'Deleteitem = txtSearchsave.Text' should not be inside the loop - it never changes.   You should not be using a streamwriter and FileIO on the same file at the same time - close the StreamWriter when you are finished with it.  (Deleteitem) does not need brackets and you should ensure the Append arguiment in WriteAllText is explicit (although that becomes a moot point).

Read a line into an array element: https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx
Write a line from an array element: https://msdn.microsoft.com/en-us/library/7ack4zyt(v=vs.110).aspx


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

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