My textbox contains 4 lines. What I want to do is separate these lines into strings i.e.
str1 = Line1
str2 = Line2
etc.
Simple enough I know
Printable View
My textbox contains 4 lines. What I want to do is separate these lines into strings i.e.
str1 = Line1
str2 = Line2
etc.
Simple enough I know
This will produce an array of lines for you called strArr()
VB Code:
Dim strArr() As String strArr = Split(Text1.Text, vbCrLf)
If your textbox Multiline property is set to True and Text1.Text doesn't have an explicit entry such vbNewLine/vbCrLf or similar then strArr = Split(Text1.Text, vbCrLf) will result only 1(one) entry in your array: Text1.Text as a single string value.
Thanks, but how do I find out how many elements are in strArr after I've done the split?
If you know how many characters are in each line then how about...
VB Code:
Dim strText as String Dim strArr() as String Dim i,j as Integer ''j is the number of characters per line j = 5 strText = Text1.Text While strText <> "" Redim Preserver strArr(i) strArr(i) = Mid$(1, strText, j) strText = Mid$(j + 1, strText, Len(strText) - j) i = i + 1 Wend
Use the UBound function...Quote:
Originally posted by mel_flynn
Thanks, but how do I find out how many elements are in strArr after I've done the split?
VB Code:
Dim elements as Integer elements = UBound(strArr) 'a looping example For i = LBound(strArr) to UBound(strArr) ''do something Next
As seoptimizer2001 said, you would use the UBound() function on the strArr array :Quote:
Originally posted by mel_flynn
Thanks, but how do I find out how many elements are in strArr after I've done the split?
VB Code:
UBound(strArr) '' zero based count of elements
Cheers;)