0

How can I search for a string inside another one and then select all the characters till end of line ? For example, given this string:

PrinterName: PDFCreator

PortName: PDFCreator:

Status: Unknown

DriverName: PDFCreator

PrinterName: Lexmark E360dn XL

PortName: someport

Status: Unknown

DriverName: Lexmark E360dn XL

HostAddress: somehostaddress

I'd like to search the string: "PrinterName" once it finds it, add it into a combobox, in order to get only the PrinterName. So far i wrote this:

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

where tmp.Text is a long string like the example above.

When i run it I get something like this:

PrinterName: PDFCreator

PrinterName: Lexmark E3

I don't get the "360dn XL"

3 Answers 3

1

Have you given any thought to using Regex? You can use a pattern like:

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

Which should find the line in your long string and capture the data into group 1. You would access the result like this:

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

Results:

Lexmark E360dn XL

You would add Matcher.Groups(1) to your combobox.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I didn't know the existence of Regex; u opened my mind . Tank you again sir.
1

This involves some simple parsing using the IndexOf and SubString Extension Methods. Here is an example that puts all of the PrinterName values into a List(of 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 Comment

Thank you, I really appreciate your answer, but i'm giving the answer to Shar1er80 cause i didn't know the existence of Regex.
1

Another one...

    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

This could also be written as:

    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 = From printer In TextSearch.Split(vbCrLf.ToCharArray)
                   Where printer.ToLower.StartsWith("printername")
                   Select printer.Split(":").Last
    For Each printer As String In printers
        Debug.Print(printer)
    Next

2 Comments

Thank you, I really appreciate your answer, but i'm giving the answer to Shar1er80 cause i didn't know the existence of Regex.
No problem. I really like this RegEx Tutorial site myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.