If you're anything like me, you probably detest string manipulation in VBScript. String manipulation in VBScript is a tedious and sometimes intimidating task. I have developed a suite of functions that combine the typical functions used in the course of extracting pieces from strings.
Don't allow yourself to be intimidated! Keep reading to find out how you can rid yourself of fear and de-convolute your code!
These functions are simple, easy to substitute into existing code, and probably similar to functions many of you have already written (or always wanted to set aside the time to write).
Let's a take a typical scenario. You need to retrieve the left part of a string of text, before some known character. For example, you need to extract the characters before the hyphen in the following string "12345-6789". This string is of variable length so left(var1, 5) won't always work. You end up writing code that looks something like this:
var2 = left(var1, (instr(var1, "-") - 1))
Invariably you never need the delimiting character so the " - 1" become a staple in this type of operation. A much cleaner implementation would look like:
var2 = leftstr(var1, "-")
Similarly, taking the right side of a string is tedious, complex, and ends up similar to:
var2 = right(var1, (len(var1) - instr(var1, "-")))
But once we add a little bit of magic:
var2 = rightstr(var1, "-")
For the monster of all complex VBScript string maniuplations ... drumroll ... Mid()! The Mid() function allows you to extract a defined number of characters from a starting point. To retreive the text part we want from the following string "<strong>Hello World</strong>", you write the following:
var2 = mid(var1, (instr(var1, ">") + 1), (instr(instr(var1, ">"), var1, "<") - instr(var1, ">") - 1))
Invaraibly with a line of code this tedious and cryptic, you end up dropping a parenthesis or accidentally grabbing an extra character from either the left side, right side, or both.
We can significantly reduce the complexity of this example by sprinkling some of our magic dust on it too:
var2 = midstr(var1, ">", "<")
Wow! Are you excited yet? Are you interested in seeing the internals of these simple yet incredibly de-convoluting scripts? Please see below:
FUNCTION leftstr(byval str, byval char)
IF (instr(str, char) > 0) THEN
leftstr = left(str, (instr(str, char) - len(char)))
END IF
END FUNCTION
FUNCTION rightstr(byval str, byval char)
IF (instr(str, char) > 0) THEN
rightstr = right(str, (len(str) - instr(str, char)))
END IF
END FUNCTION
FUNCTION midstr(byval str, byval char1, byval char2)
IF (instr(str, char1) > 0 AND instr(str, char2) > 0) THEN
midstr = mid(str, (instr(str, char1) + len(char1)), (instr((instr(str, char1) + len(char1)), str, char2) - (instr(str, char1) + len(char1))))
END IF
END FUNCTION
As you can plainly see from the code above, I utilized the functions themselves instead of looping through the characters. I'm not big on re-inventing perfectly functional wheels. I just don't want to have to use 12 open and close parenthesis characters to simply extract part of a text string. Implementing these two functions creates two distinct advantages:
1.) By abstracting yourself from the tedious placement of instr() and len() functions, riddled w/ confusing - 1 and + 1 snippets, your code becomes easier to read, write, and troubleshoot.
2.) The placement of a quick check to ensure each character exists in the target string will prevent errors that arise from ill-begotten strings not containing your expected delimiters.
Don't overlook the fact that the delimiters can be any length, so you can do something like this:
acc_phone = midstr(html, "<td class=""acc_phone"">", "</td>")
In fact a few years ago I was able to scrape thousands of records from a website (these actions were legal and sanctioned by the website in question) in about five (5) minutes through writing only a few short lines of code. It looked remarkably similar to the flow below:
1.) Retrieve the response HTML from the web page.
html = http.responsetext '(from XMLHttpDocument)
2.) Split the text on some record-delimiting character set, typically something like "</tr><tr>".
html_array = split(html, "</tr><tr>")
3.) Loop through the array and extract the pieces you need without fear or hassle!
acc_id = midstr(html_array(a), "<td class=""acc_id"">", "</td>")
acc_phone = midstr(html_array(a), "<td class=""acc_phone"">", "</td>")
acc_email = midstr(html_array(a), "<td class=""acc_email"">", "</td>")
acc_address1 = midstr(html_array(a), "<td class=""acc_address1"">", "</td>")
acc_address2 = midstr(html_array(a), "<td class=""acc_address2"">", "</td>")
acc_city = midstr(html_array(a), "<td class=""acc_city"">", "</td>")
acc_state = midstr(html_array(a), "<td class=""acc_state"">", "</td>")
acc_zip = midstr(html_array(a), "<td class=""acc_zip"">", "</td>")
acc_country = midstr(html_array(a), "<td class=""acc_country"">", "</td>")
How easy is that? Now go get programming and I'll be posting again later this week with even more useful functions that should be part of every ASP developer's toolkit!

Comments