get string array length
-
does anyone of you know how to get the length of a string array? My code looks like this
mystring = ActiveDocument.Paragraphs(index) mydata = Split(mystring, " ")
So in the beginning I do not know the size of the array. But I do need to know if the word I am searching for, is in this array. Therefor I have to parse the array to search it. But if I do not know the upper limit, this causes a "out of bounds" error. So I do need the upper limit to avoid the error to be thrown. Could anyone please tell me how to get the length so I could do the following (in VBA!) ?For i = 0 to upperlimit if mydata(i) = mysearchstring then do something end if Next
Thanks! Stephan. -
does anyone of you know how to get the length of a string array? My code looks like this
mystring = ActiveDocument.Paragraphs(index) mydata = Split(mystring, " ")
So in the beginning I do not know the size of the array. But I do need to know if the word I am searching for, is in this array. Therefor I have to parse the array to search it. But if I do not know the upper limit, this causes a "out of bounds" error. So I do need the upper limit to avoid the error to be thrown. Could anyone please tell me how to get the length so I could do the following (in VBA!) ?For i = 0 to upperlimit if mydata(i) = mysearchstring then do something end if Next
Thanks! Stephan. -
Try
UBound(mydata,1)
An optimist's glass is half full. A pesimist's glass is half empty. An engineer goes and gets the right size glass.thanks, it works! Stephan.
-
does anyone of you know how to get the length of a string array? My code looks like this
mystring = ActiveDocument.Paragraphs(index) mydata = Split(mystring, " ")
So in the beginning I do not know the size of the array. But I do need to know if the word I am searching for, is in this array. Therefor I have to parse the array to search it. But if I do not know the upper limit, this causes a "out of bounds" error. So I do need the upper limit to avoid the error to be thrown. Could anyone please tell me how to get the length so I could do the following (in VBA!) ?For i = 0 to upperlimit if mydata(i) = mysearchstring then do something end if Next
Thanks! Stephan.There are at least 3 ways to do what you want: 1. As already mentioned, use ubound. ubound is a vb only feature and is convenient because it returns the upper bound of the array which is usefull for loops. 2. mydata.Length basically returns the count of array items. This available in any language of .net. It is less convenient for loops because you must subtract 1 from it. 3. For Each loop:
For Each str As String In MyData
'Do Something
NextYou don't have to know the array bounds to loop through the items.