Results 1 to 4 of 4

Thread: Search string to add line after specific word

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    3

    Question Search string to add line after specific word

    (I am a VERY basic programmer)


    I have a body of text which needs searching through to find a specific word "Customer" and then save the next line as the CustomerName (CustomerName = <line of text>)


    Please help!
    Thank you

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Search string to add line after specific word

    Hi,

    Since you say:-

    save the next line as the CustomerName (CustomerName = <line of text>)
    This implies that the data your are working with has Carriage Return / Line Feed characters in it so in this example I create a string with CRLF characters in it and then use the Split method of the String class to split the Data into lines using the CRLF characters. This can then be iterated through to look for the word "Customer" on a line and, once found, we assign the NEXT line to the myCustomerName variable.

    Code:
    Public Class Form1
      Private Const myData As String = _
        "Customer ID" & vbCrLf & _
        "ABC123" & vbCrLf & _
        "Customer" & vbCrLf & _
        "ABC Company Ltd" & vbCrLf & _
        "Telephone No" & vbCrLf & _
        "01245 4578964" & vbCrLf
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim myDataLines() As String = myData.Split(CChar(vbCrLf))
        Dim myCustomerName As String = String.Empty
    
        For Counter As Integer = 0 To myDataLines.Count - 1
          If myDataLines(Counter).Trim = "Customer" Then
            myCustomerName = myDataLines(Counter + 1).Trim
          End If
        Next
        MsgBox(myCustomerName)
      End Sub
    End Class
    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    3

    Smile Re: Search string to add line after specific word

    Hey Ian,

    I'm sorry, I should have given an example of what the code has to search for:

    some text

    exampleemail1@example.com

    exampleemail2@example.com

    Customer
    Mr Blobby

    More text etc etc etc blah blah blah


    And it would need to extract "Mr Blobby" and save it as "CustomerName"


    Thank you very much for the code so far! But I'm guessing there is an easier way to do it?

    Thanks,

    Conna
    Last edited by 6bramleycs; Mar 11th, 2013 at 05:13 AM. Reason: added incorrect information

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Search string to add line after specific word

    Hi,

    Sorry, but I do not understand what you are asking for which is different to what I have already posted so far. You have asked to get the Customer, which is on the following line, after finding the word Customer. So what's changed in your second post?

    I think you need to expand, in DETAIL, what you are trying to do and what structure you are dealing with.

    Cheers,

    Ian

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width