-
Mar 8th, 2013, 04:59 AM
#1
Thread Starter
New Member
-
Mar 8th, 2013, 05:38 AM
#2
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
-
Mar 11th, 2013, 05:12 AM
#3
Thread Starter
New Member
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
-
Mar 11th, 2013, 08:32 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|