在文本框中搜索字符串后查找字符串

时间:2015-04-21 17:04:56

标签: vb.net sockets textbox

我一直在使用一个应用程序,它使用套接字连接到交换机并发出命令。之后,从交换机读回的数据将放入文本文件中,并且包含特定字符串的行(例如"GE1")将放置在如下文本框中。

GE1/0/1              UP   1G(a)   F(a)   A    101  To Station 1
GE1/0/2              DOWN auto    A      A    1    To Station 2
GE1/0/3              DOWN auto    A      A    1    To Station 3
GE1/0/4              DOWN auto    A      A    1    To Station 4
GE1/0/5              DOWN auto    A      A    1    To Station 5
GE1/0/6              DOWN auto    A      A    1    To Station 6
GE1/0/7              DOWN auto    A      A    1    To Station 7
GE1/0/8              DOWN auto    A      A    1    To Station 8
  ---- More ----[16D                [16DGE1/0/9              DOWN auto    A      A    1    To Station 9
  ---- More ----[16D                [16DGE1/0/10             DOWN auto    A      A    1    To Station 10
  ---- More ----[16D                [16DGE1/0/11             DOWN auto    A      A    1    To Station 11
  ---- More ----[16D                [16DGE1/0/12             DOWN auto    A      A    1    To Station 12
  ---- More ----[16D                [16DGE1/0/13             DOWN auto    A      A    1    To Station 13
  ---- More ----[16D                [16DGE1/0/14             DOWN auto    A      A    1    To Station 14
  ---- More ----[16D                [16DGE1/0/15             DOWN auto    A      A    1    To Station 15
  ---- More ----[16D                [16DGE1/0/16             DOWN auto    A      A    1    To Station 16
  ---- More ----[16D                [16DGE1/0/17             DOWN auto    A      A    1    To Station 17
  ---- More ----[16D                [16DGE1/0/18             DOWN auto    A      A    1    To Station 18
  ---- More ----[16D                [16DGE1/0/19             DOWN auto    A      A    1    To Station 19
  ---- More ----[16D                [16DGE1/0/20             DOWN auto    A      A    1    To Station 20
  ---- More ----[16D                [16DGE1/0/21             DOWN auto    A      A    1    To Station 21
  ---- More ----[16D                [16DGE1/0/22             DOWN auto    A      A    1    To Station 22
  ---- More ----[16D                [16DGE1/0/23             DOWN auto    A      A    1    To Station 23
  ---- More ----[16D                [16DGE1/0/24             DOWN auto    A      A    1    To Station 24
  ---- More ----[16D                [16DGE1/0/25             DOWN auto    A      A    1    To Station 25
  ---- More ----[16D                [16DGE1/0/26             DOWN auto    A      A    1    To Station 26

问题如下:我想在GE1 / 0 /之后取不同的文字?并将每个单独的文本应用于标签,以便可以在小表单上查看信息。

示例:

Label1 = "UP" 
Label2 = "1G(a)
'and so on until it reads to the end of the line before moving to the next line and
'repeating with the next group of labels. 

请注意,信息是否有东西连接到电台。

旁注:我尝试使用Textbox1.text = Textbox1.text.Replace(" ---- More ----[16D [16D", "")时使用.Trim,但不删除文本。建议?

1 个答案:

答案 0 :(得分:1)

您可以将其称为text file,然后是textbox。这似乎是一个多行文本框,所以你应该使用lines数组。更好的方法是将文件加载到数组中,处理它,然后发布到TextBox或直接解析它。

对于修剪第一部分,无论它是多长时间都会切断第一部分,无论它是否真的是空格或标签或其他内容。

    Dim lines = TextBox1.Lines    ' or from file

    Dim ndx As Integer

    For n As Integer = 0 To lines.Length - 1
        If lines(n).StartsWith("GE1") = False Then
            ndx = lines(n).IndexOf("GE1")
            lines(n) = lines(n).Substring(ndx, lines(n).Length - ndx)
        End If
    Next

    TextBox1.Lines = lines

由于此出现或多或少是固定长度字段,因此您应该能够使用您喜欢的文本解析器在清理行数据后将其删除。我建议将其解析为一个类,以便您可以获取任何数据元素所需的块。作为解析的一个例子,这将把它切换到明显的字段并显示在ListView中。

Dim ndx As Integer
Dim tmp As String
Dim lvi As ListViewItem

For n As Integer = 0 To lines.Length - 1
    If lines(n).StartsWith("GE1") = False Then
        ndx = lines(n).IndexOf("GE1")
        lines(n) = lines(n).Substring(ndx, lines(n).Length - ndx)
    End If

    tmp = lines(n)
    ' col 1 = 10 chars (approx)
    lvi = New ListViewItem(tmp.Substring(0, 10).Trim)
    tmp = tmp.Substring(10, tmp.Length - 10).Trim()

    ' col 2 = up to 4 chars
    lvi.SubItems.Add(tmp.Substring(0, 4))
    tmp = tmp.Substring(4, tmp.Length - 4).Trim()

    ' col 3 = up to 5 chars "1G(a)"
    lvi.SubItems.Add(tmp.Substring(0, 5))
    tmp = tmp.Substring(5, tmp.Length - 5).Trim()

    ' col 4 = up to 4 chars "F(a)"
    lvi.SubItems.Add(tmp.Substring(0, 4))
    tmp = tmp.Substring(4, tmp.Length - 4).Trim()

    lvi.SubItems.Add(tmp.Substring(0, 1))
    tmp = tmp.Substring(1, tmp.Length - 1).Trim()

    lvi.SubItems.Add(tmp.Substring(0, 3))
    tmp = tmp.Substring(3, tmp.Length - 3).Trim()

    lvi.SubItems.Add(tmp)

    lv1.Items.Add(lvi)
Next

注意:提供的数据样本不全面,HTML格式可能会进行一些更改,因此可能需要调整确切的代码。否则:

enter image description here enter image description here