如何在 vb .net 的多行文本框中获取特定单词的行数
How to get number of line of a specific word in it in multiline textbox in vb .net
我有一个多行文本框,我想获取其中特定单词的行号。
我试过这个:
For Each line As String In TextBox1.Lines
If line = "50" Then
Label2.Text = 'Number Of line
End If
Next
但我不知道如何获取其中“50”的行号并将其显示在label2 中。
我该怎么做?
使用 For
循环代替 For Each
:
Dim lines = TextBox1.Lines
For i As Int32 = 0 To lines.Length - 1
If lines(i) = "50" Then Label2.Text = (i + 1).ToString()
Next
我将 TextBox.Lines
String()
存储在一个变量中,因为如果你经常使用这个 属性,就会有 some overhead。
尝试使用计数器:
Dim iLineCount As Integer = 0
For Each line As String In TextBox1.Lines
iLineCount += 1
If line = "50" Then
Label2.Text = iLineCount.ToString()
End If
Next
或者试试这个:
Dim lines = TextBox1.Lines
Label2.Text = Array.IndexOf(lines, "50").ToString()
这将显示第一行的(基于零的)索引包含“50”。如果没有找到匹配的行,则为 -1。
我有一个多行文本框,我想获取其中特定单词的行号。
我试过这个:
For Each line As String In TextBox1.Lines
If line = "50" Then
Label2.Text = 'Number Of line
End If
Next
但我不知道如何获取其中“50”的行号并将其显示在label2 中。 我该怎么做?
使用 For
循环代替 For Each
:
Dim lines = TextBox1.Lines
For i As Int32 = 0 To lines.Length - 1
If lines(i) = "50" Then Label2.Text = (i + 1).ToString()
Next
我将 TextBox.Lines
String()
存储在一个变量中,因为如果你经常使用这个 属性,就会有 some overhead。
尝试使用计数器:
Dim iLineCount As Integer = 0
For Each line As String In TextBox1.Lines
iLineCount += 1
If line = "50" Then
Label2.Text = iLineCount.ToString()
End If
Next
或者试试这个:
Dim lines = TextBox1.Lines
Label2.Text = Array.IndexOf(lines, "50").ToString()
这将显示第一行的(基于零的)索引包含“50”。如果没有找到匹配的行,则为 -1。