admin
06-06-2003, 01:54 PM
If you're not familiar with strings and arrays, then this article will give you a solid introduction.
You can "Split" a string into multiple parts and create an array. By splitting a string you automatically create an array. Look at the following code example:
myString = "This is a test and only a test"
myArray = Split(myString)
What you have now is an array named myArray as follows...
This
is
a
test
and
only
a
test
In array notation this would be the contents of myArray...
myArray(0) 'would = This
myArray(1) 'would = is
myArray(2) 'would = a
myArray(3) 'would = test
myArray(4) 'would = and
myArray(5) 'would = only
myArray(6) 'would = a
myArray(7) 'would = test
You could use a For Loop to cycle through the array elements and display them. You use the UBound() function to find the upper most array element...
For i = 0 To UBound(myArray)
Response.Write myArray(i) & "<br>"
Next
Or, you could manipulate the array elements...
For i = 0 To UBound(myArray)
If myArray(i) = "a" Then myArray(i) = "one"
Next
To put the array back into it's original form, a string, you use the Join() function like so...
newString = Join(myArray)
Using the example where we tested for the word "a" and replaced it with "one" our new string would look like this:
newString = "This is one test and only one test"
By default, the Split() function breaks a string apart by spaces. An optional argument of the Split() function allows you to break the string apart by another character... for example a comma:
myString = "No matter where you go, there you are"
myArray = Split(myString, ",")
This will split the string whenever a comma is encountered. Thus myArray would now look like this...
No matter where you go
there you are
A third optional argument of the Split() function allows you to limit how many elements the Split() function returns...
myString = "All, some, none"
myArray = Split(myString, "," , 2)
Since only two elements were specified myArray would now look like so...
All
some
"none" would be left out since only the first two elements were allowed.
You can also filter the array. Using the Filter() function, you can filter out any array element that doesn't contain a certain letter...
myString = "Hakeem is the Dream if you are a Rockets fan"
myArray = Split(myString)
myArray = Filter(myArray, "f")
Since only two words have the letter "f" in them myArray would now look like this...
if
fan
By default the Filter() function is set to TRUE. Meaning that any array element that matched the filter, in this case the letter "f", would be kept and the non-matching elements thrown out. An optional argument allows you to do jut the opposite:
myArray = Filter(myArray, "f", False)
The result would be...
Hakeem
is
the
Dream
you
are
a
Rockets
We're hoping you learned something about strings and arrays. Enjoy.
You can "Split" a string into multiple parts and create an array. By splitting a string you automatically create an array. Look at the following code example:
myString = "This is a test and only a test"
myArray = Split(myString)
What you have now is an array named myArray as follows...
This
is
a
test
and
only
a
test
In array notation this would be the contents of myArray...
myArray(0) 'would = This
myArray(1) 'would = is
myArray(2) 'would = a
myArray(3) 'would = test
myArray(4) 'would = and
myArray(5) 'would = only
myArray(6) 'would = a
myArray(7) 'would = test
You could use a For Loop to cycle through the array elements and display them. You use the UBound() function to find the upper most array element...
For i = 0 To UBound(myArray)
Response.Write myArray(i) & "<br>"
Next
Or, you could manipulate the array elements...
For i = 0 To UBound(myArray)
If myArray(i) = "a" Then myArray(i) = "one"
Next
To put the array back into it's original form, a string, you use the Join() function like so...
newString = Join(myArray)
Using the example where we tested for the word "a" and replaced it with "one" our new string would look like this:
newString = "This is one test and only one test"
By default, the Split() function breaks a string apart by spaces. An optional argument of the Split() function allows you to break the string apart by another character... for example a comma:
myString = "No matter where you go, there you are"
myArray = Split(myString, ",")
This will split the string whenever a comma is encountered. Thus myArray would now look like this...
No matter where you go
there you are
A third optional argument of the Split() function allows you to limit how many elements the Split() function returns...
myString = "All, some, none"
myArray = Split(myString, "," , 2)
Since only two elements were specified myArray would now look like so...
All
some
"none" would be left out since only the first two elements were allowed.
You can also filter the array. Using the Filter() function, you can filter out any array element that doesn't contain a certain letter...
myString = "Hakeem is the Dream if you are a Rockets fan"
myArray = Split(myString)
myArray = Filter(myArray, "f")
Since only two words have the letter "f" in them myArray would now look like this...
if
fan
By default the Filter() function is set to TRUE. Meaning that any array element that matched the filter, in this case the letter "f", would be kept and the non-matching elements thrown out. An optional argument allows you to do jut the opposite:
myArray = Filter(myArray, "f", False)
The result would be...
Hakeem
is
the
Dream
you
are
a
Rockets
We're hoping you learned something about strings and arrays. Enjoy.