
Getting a single line out of a Variant (<=-- or something like that)
On Mon, 08 Dec 1997 14:51:36 +0100, John Brookman
Quote:
>Hi!
>Thanks for reading my message.
>I have a small question:
>"How do I get a single line out of a variant in VB 5.0?)
>For Example:
>Br = Chr(13) + Chr(10)
>test= "Hello" & Br & "How are you today?" & br & "Fine, Thank you"
>How do I get the second, third or first line out of 'test' ?
>I'm just a newbie so my example will be very low-programmed, but hey I
>had to think of some example.
>Thanks,
> John Brookman
John-
Here is a calling "test" function and a function to do what
you're after.. It's not pretty or optimized at all.. Before I get
rebuked, I should mention that there are FAR more eligant ways to pull
this off using InStr() and a bit of thought.. But I just thought I'd
give you the "brute force" code with (hopefully) easy to read variable
names.. Hope this helps ya out!
-Keith Hyer
Sub Calling_Test_Function()
Dim test As Variant
Dim br As String
Dim result As String
Dim LineNumber As Integer 'I'm assuming you won't want more than_
32k "lines" per variant..
br = Chr$(13) & Chr$(10)
test = "Now is the time for..." & br & br & "uhm... Stuff and_
stuff to happen.." & br & "(..and stuff)"
MsgBox test
LineNumber = 3
result = GimmeDatLineNumber(CStr(test), LineNumber)
MsgBox result
MsgBox "That wasn't too painful, now.. Was it?", 292, "Eh?"
End Sub
Function GimmeDatLineNumber(line As String, linenum As Integer) As_
String
'Line is the string in which you're searching...
'LineNum is the line number you seek..
Dim lstart As Integer '"Current line"'s starting position
Dim lend As Integer '"Current Line"'s Ending position (so we_
can reference where next line begins)
Dim dlen As Integer '"Current line"'s length (within the_
string)
Dim OnChar As Integer 'Which character in the string am I on..
Dim br As String
br = Chr$(13) 'I'm only going to search for one character at a_
time.. You can expand if you wish..
Dim lineIAmOn As Integer 'Which line am I on?
lstart = 1
dlen = 1
lineIAmOn = 1
OnChar = 1
'Stop
Do While OnChar <= Len(line)
If Mid$(line, OnChar, 1) = br Then
lend = (OnChar - 1) 'The one right before the CR/LF_
start
If lineIAmOn = linenum Then
GimmeDatLineNumber = Mid$(line, lstart, dlen)
Exit Function
Else
lstart = lend + 2 'skip both the CR and the LF
End If
OnChar = OnChar + 1 'Advance
lineIAmOn = lineIAmOn + 1
dlen = 1
Else
OnChar = OnChar + 1
dlen = dlen + 1
End If
Loop
GimmeDatLineNumber = "" 'Or you could put something fixed like,_
"ERR" here if you wish..
End Function