首页 > 解决方案 > 从 CSV VB.net 中搜索数组的功能

问题描述

所以我做了一个函数的一部分,它读取 CSV 文件中的所有名称并将其输入到屏幕上,但现在我需要制作它,以便用户可以输入要搜索的名称,如果它在 CSV 文件中,那么它将告诉他们名字在文件中。这是我到目前为止所拥有的:

CSV 中的名称:

    jack
    tom
    jeff
    bill
    bob

读取 csv 中的名称:

Dim location As String = "C:\Users\626284\Desktop"
    Dim files As String = "names.csv"
    Dim lines = File.ReadAllLines(Path.Combine(location, files))
    System.Array.Sort(lines)

    Console.WriteLine(String.Join(Environment.NewLine, lines))

搜索名称:Havent 解决了这部分问题。基本上程序不得不说

Console.WriteLine("Please enter a name you would like to search for")
search = Console.ReadLine()
If search = lines
then 
Console.WriteLine("Name is found")
ElseIf 
Console.WriteLine("Name isnt found")

标签: vb.net

解决方案


我会这样做:

Console.WriteLine("Please enter a name you would like to search for")
Dim search = Console.ReadLine()
If lines.Any(Function(line) line = search) Then
    Console.WriteLine("Name is found")
ElseIf 
    Console.WriteLine("Name isnt found")
End If

推荐阅读