Wednesday, 21 March 2012

ASP: the split function and arrays

   


The ASP split function is used to - guess what - split a string into specific parts. The resulting number of items is variable and depends on how we use the function.
Basically the split function has the following syntax:
VarArray = split(string, delimiter, numberOfItems)
where:
1) string is the string to be split;
2) delimiter is the element used to determine where to split the string;
3) numberOfItems is optional and limits the number of returned items in the array.

Let's see some examples.
<%
Dim VarString
Dim VarArray
VarString = "Welcome to The Web Thought!"
VarArray = split(VarString, " ")
For each item in VarArray
  response.write(item & "<br>")
Next
%>
The output of the above code will be:
Welcome
to
The
Web
Thought!
If we change the split function and add a limit to the number of items in the array:
split(VarString, " ", 3)
the output will be:
Welcome
to
The Web Thought!
Having said that, the split function might become very useful if we need to store dynamic information in a database and retrieve them storing them in an array. We can, for example, store a list of available colour codes for a specific product, separating them by a semicolon:
A1;A2;A3;A4
then we can store the recordset field item in a string and split it using ";" as delimiter:
VarArray = split("A1;A2;A3;A4", ";")
The output will be:
A1
A2
A3
A4

I hope you've found the above information useful for your projects. In the meantime, happy programming!

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.