比较VB Net中的多行字符串

时间:2019-02-06 18:56:28

标签: vb.net

我有一个名为Textbox1的文本框控件,其中包含以下项(在多行字符串中):

22,23
57,58
20,21
51,52
57,58
20,21
21,22
25,26
35,36
41,42
50,51
22,23
23,24
37,38
44,45
45,46
67,68
72,73
78,79

如何开始计算这些值?例如,20,21出现两次,22,23也出现两次,依此类推。

文本框中的每一行包含两个值,两个值之间用逗号分隔。我想将每一行与所有其他行进行比较,如果存在另一个具有相同值的实例,则将其添加到计数中。然后,我想在一组单独的文本框中显示每个值的计数:总共40个文本框,每个值一个。输出文本框将被编号以显示该值,如下所示:

TxtConsecutiveCount1.Text = "(1,2)"
TxtConsecutiveCount2.Text = "(3,4)"
TxtConsecutiveCount3.Text = "(5,6)"

但是,我还需要显示每个中的总数。

我尝试什么:

Private Sub NumberConsecutive1()
    Try
        Dim number As Integer
        Dim numbers As New List(Of Integer)
        For Each strNum As String In TxtIntDraws.Lines(1).Split(",")
            If Integer.TryParse(strNum.Trim, number) Then
                numbers.Add(number)
            End If
        Next
        numbers.Sort()
        For seriesLength As Integer = 2 To numbers.Count
            Dim ctl As Control = Me.Controls.Find("txtConsecutiveNumber" & seriesLength, True).FirstOrDefault
            If TypeOf (ctl) Is TextBox Then
                Dim tb As TextBox = DirectCast(ctl, TextBox)
                For i As Integer = 0 To (numbers.Count - seriesLength)
                    If (numbers(i + (seriesLength - 1)) - numbers(i)) = (seriesLength - 1) Then
                        tb.AppendText(String.Join(",", numbers.GetRange(i, seriesLength)) & vbCrLf)
                    End If
                Next
            End If
        Next
    Catch ex As Exception
    End Try
    NumberConsecutive2()
End Sub

0 个答案:

没有答案