VB NET字符串搜索

时间:2015-07-29 20:21:55

标签: vb.net

如何在另一个字符串中搜索字符串,然后选择所有字符直到行尾? 例如,给定此字符串:

PrinterName:PDFCreator

PortName:PDFCreator:

状态:未知

DriverName:PDFCreator

PrinterName:Lexmark E360dn XL

PortName:someport

状态:未知

DriverName:Lexmark E360dn XL

HostAddress:somehostaddress

我想搜索字符串:“PrinterName”一旦找到它,将其添加到组合框中,以便只获取PrinterName。 到目前为止我写了这个:

Dim TextSearched As String = tmp.Text
    Dim Paragraph As String = "PrinterName:"
    Dim location As Integer = 0
    Dim occurances As Integer = 0

    Do

        location = TextSearched.IndexOf(Paragraph, location)

        If location <> -1 Then

            occurances += 1
            If TextSearched.EndsWith(vbCrLf) Then

                Debug.Print(TextSearched.Substring(location, TextSearched.IndexOf(vbCrLf)))

            End If
            location += Paragraph.Length

        End If



    Loop Until location = -1

其中tmp.Text是一个长字符串,如上例所示。

当我运行它时,我得到这样的东西:

PrinterName:PDFCreator

PrinterName:Lexmark E3

我没有得到“360dn XL”

3 个答案:

答案 0 :(得分:1)

这涉及使用IndexOf和SubString Extension Methods进行一些简单的解析。下面是一个将所有PrinterName值放入List(String)的示例。:

    Dim lstLines As List(Of String) = IO.File.ReadAllLines("C:\Your\Location\tmp.txt").ToList()

    Dim lstPrinters As New List(Of String)

    lstLines.ForEach(Sub(strLine As String)
                         If strLine.IndexOf("PrinterName:") > -1 Then
                             lstPrinters.Add(strLine.Substring(strLine.IndexOf("PrinterName:") + 13))
                         End If
                     End Sub)

答案 1 :(得分:1)

您是否考虑过使用Regex?您可以使用如下模式:

"PrinterName: (.*?)\r\n"

哪个应该在长字符串中找到该行并将数据捕获到组1中。您可以像这样访问结果:

Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
        Dim TextSearch As String = _
            "PrinterName : PDFCreator()" + vbCrLf + _
            "PortName: PDFCreator()" + vbCrLf + _
            "Status: Unknown()" + vbCrLf + _
            "DriverName: PDFCreator()" + vbCrLf + _
            "PrinterName: Lexmark E360dn XL" + vbCrLf + _
            "PortName: someport()" + vbCrLf + _
            "Status: Unknown()" + vbCrLf + _
            "DriverName: Lexmark E360dn XL" + vbCrLf + _
            "HostAddress: somehostaddress()"

        Dim Matcher = Regex.Match(TextSearch, "PrinterName: (.*?)\r\n")
        If Matcher.Success Then
            Console.WriteLine(Matcher.Groups(1))
        End If
    End Sub
End Module

结果:

Lexmark E360dn XL

您可以将Matcher.Groups(1)添加到组合框中。

答案 2 :(得分:1)

另一个......

FormsAuthentication.SetAuthCookie(username, "false");

这也可以写成:

    Dim TextSearch As String =
        "PrinterName : PDFCreator()" + vbCrLf +
        "PortName: PDFCreator()" + vbCrLf +
        "Status: Unknown()" + vbCrLf +
        "DriverName: PDFCreator()" + vbCrLf +
        "PrinterName: Lexmark E360dn XL" + vbCrLf +
        "PortName: someport()" + vbCrLf +
        "Status: Unknown()" + vbCrLf +
        "DriverName: Lexmark E360dn XL" + vbCrLf +
        "HostAddress: somehostaddress()"

    Dim printers As List(Of String) = TextSearch.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries).Where(Function(x) x.ToLower.StartsWith("printername")).Select(Function(x) x.Split(":").Last).ToList
    For Each printer As String In printers
        Debug.Print(printer)
    Next