如何从文件读取并在文件上搜索用户的输入? VB.NET

时间:2018-09-29 10:01:40

标签: vb.net

这是我的代码,我想搜索用户的输入,即某个.txt文件上的字符串,如果找到,则显示找到日期,否则找不到输入。

Dim freader As IO.StreamReader
    Dim strline, a As String
    freader = New IO.StreamReader(" C:\Users\neWbie889\Documents\vb\strings.txt")
    strline = freader.ReadLine
    Do While Not strline Is Nothing
        strline = freader.ReadLine()
    Loop
    Console.WriteLine("enter your string")
    a = Console.ReadLine()
    If strline = a Then
        Console.WriteLine("input found")
    ElseIf strline <> a Then
        Console.WriteLine("input not found")
    End If
    freader.Close()

文本文件按以下顺序包含数据:

750401 234523

456465 345345

054156 34534

023156 534543

156456 435345

2 个答案:

答案 0 :(得分:0)

您可以将文件中的所有文本读取为字符串:

Dim allText As String = File.ReadAllText("path to file")

,然后检查用户使用Contains方法提供的字符串:

If allText.Contains(a) = True Then
    Console.WriteLine("input found")
Else
    Console.WriteLine("input not found")
End If

答案 1 :(得分:0)

 Imports System.IO
 Module Module1
 Sub Main()
    Dim a As String
    Dim allText As String = File.ReadAllText("C:\Users\KronosXtitan\Documents\vb\ddates.txt")
    Console.WriteLine("enter a number")
    a = Console.ReadLine()
    If allText.Contains(a) = True Then
        Console.WriteLine("the number was found")
    Else
        Console.WriteLine("the number was not found")
    End If


End Sub

End Module

感谢γηράσκωδ'αείπολλάδιδασκόμε帮助我解决此问题