如何从文本文件 vb.net 中删除重复的行 [英] how to remove duplicate line from text file vb.net

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

问题描述

我想从包含 for 的文本文件中删除重复的行示例:

I want to remove duplicated line from text file that contain for example :

.abc
.def
.ghi
.abc
.abc

得到结果

.abc
.def
.ghi

推荐答案

使用 .Add()HashSet(Of T) 方法,添加只设置新项目.HashSet(Of T) 类 >

Use .Add() method of HashSet(Of T) which adding to set only new items. HashSet(Of T) Class

Dim path As String = "yourPath"
Dim lines As New HashSet(Of String)()
'Read to file
Using sr As StreamReader = New StreamReader(path)
    Do While sr.Peek() >= 0
        lines.Add(sr.ReadLine())
    Loop
End Using

'Write to file
Using sw As StreamWriter = New StreamWriter(path)
    For Each line As String in lines
        sw.WriteLine(line)
    Next
End Using

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

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