[英]Index of line textfile using StreamReader vb.net
How can I use this code?我怎样才能使用这个代码?
TextBox1.Text = Array.FindIndex(linestring, Function(s) s.Contains(something))
but to use the code, without a word, and to display my array index following the code below:但是要使用代码,一言不发,并按照以下代码显示我的数组索引:
Dim lines As New List(Of String)
Using reader As New StreamReader(My.Application.Info.DirectoryPath + ("\Data.txt"))
Dim line As String
Do Until line.StartsWith(endPrefix)
lines.Add(line)
line = reader.ReadLine()
'maybe here index of array
Loop
so how do I use this to get the line index from my text files?那么如何使用它从我的文本文件中获取行索引?
Here is an example which uses the File.ReadLines method (which enumerates the lines of a file) where you can pass the predicate for the comparison to get the line number (starting at 1) of the first match:这是一个使用File.ReadLines方法(枚举文件的行)的示例,您可以在其中传递谓词进行比较以获取第一个匹配项的行号(从 1 开始):
Imports System.IO
Module Module1
Function FindLineNumber(sourceFile As String, textToFind As String, predicate As Func(Of String, String, Boolean)) As Integer
Dim lineNo = 1
For Each l In File.ReadLines(sourceFile)
If predicate(l, textToFind) Then
Return lineNo
End If
lineNo += 1
Next
Return -1
End Function
Sub Main()
' File to look in:
Dim src = "C:\temp\population.csv"
' Text to find:
Dim find = "133"
Dim lineNum = FindLineNumber(src, find, Function(a, b) a.Contains(b))
If lineNum > 0 Then
Console.WriteLine($"""{find}"" found at line {lineNum}.")
Else
Console.WriteLine($"""{find}"" not found.")
End If
Console.ReadLine()
End Sub
End Module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.