s Visual Basic and VBScript have progressed from version to version, chances are you have not kept up to date with some of the new functions introduced in the later releases. I have met many developers who are unaware of the existence of some really useful functions in VB6 or in VBScript that could help in coding VB or ASP applications. These developers rely on older workarounds for writing code that could be handled by the new built-in functions. In this installment of Tactical ASP, I explore the new explore the new "Split" function and its myriad uses.
The Split function may not necessarily be new to you and you probably already looked at it and said, "Ho hum. Another functionwho needs it!" But waitdon't drop this function into the trash yet. The Split function can provide unexpected returns if used properly.
What's the Split?
Let's look at the function itself. The Split function, according to the documentation, "returns a zero based, one-dimensional array containing a specified number of substrings." Yes, I can hear the groans; "oh no, not another array function. I don't use no stinkin' arrays in my code anyway!"
Don't give up yet. There is hope. The Split function is defined by this syntax:
Split(expression [, delimiter [, count [, compare]]])
This is how it is explained:
- The function name is Split, and yes, it is a function, so it needs to be on the right hand side of an equal-to sign.
- "Expression" is a required string expression containing substrings and delimiters.
- "Delimiter" is an optional string identifying the delimiter character. By default, a space character (" ") is considered to be the delimiter.
- "Count" is an optional number of substrings to return. The default is -1, which indicates all substrings are to be returned.
- "Compare" is an optional numeric value signifying how the comparison should take place for evaluating substrings. A 0 indicates binary comparison; a 1 (the default) signifies textual comparison.
Given a text string, the Split function can quickly and efficiently break it down into an array of strings, based on your chosen delimiter. So, if you had the following string:
"This is the tactical ASP column by Rama Ramachandran"
the following code breaks the string into an array of strings, each containing one word.
strValue = "This is the tactical ASP column by Rama Ramachandran"
strAryWords = Split(strValue, " ")
' - strAryWords is now an array
For i = 0 to Ubound(strAryWords)
Response.Write i & " = " & strAryWords(i) & "<BR>"
The above code will produce the following output:
0 = This
1 = is
2 = the
3 = tactical
4 = ASP
5 = column
6 = by
7 = Rama
8 = Ramachandran