Collection of VBScript string functions for classic ASP

I’ve just thought of extracting another class of my framework called StringOperations and publish it on here for free use because I think there are some useful things in it which you need for everyday work. Beside that it includes some functions which are also available in the .net framework. So whenever you have to deal with classic ASP again and you want to have a more powerful base for your work then this is a must.

Just download the class and have a quick look at the StringOperations documentation (Links at the bottom of the article). I recommend you to load this class somewhere that it will be always available and you don’t have to care about its presence. Also a name like “str” sounds good and makes your code more readable now. I’ve stapled together some short examples of the most important methods of the class, all others should be self explaining…

writeln(), wirte(), end(): I use them instead of response.write and response.end. It makes my code nicer and less to write. Furthermore I am on the safe side when I want to change the output method in the future (e.g. some StringBuilder)

format(): is the almost equivalent to the string.format() of .net. The idea of such a method is brilliant and I needed it somehow for classic ASP. As there is no way of optional parameters (e.g. params in C#) I decided to use an array for the simulation of optional params. Example of usage:

str.write(str.format("my name is {0} and i am {1} years old.", array("Michal", "25")))

shorten(): hey I need this everywhere and in every project. I wonder why its not incorporated into any base framework such as .net. (or maybe I havent found it yet). This short functions helps me to shorten strings everywhere I dont want long ones. e.g. News summaries, long titles, etc. It expects the maximum length and the string which will be used to indicate the “overflow”. Example, which will shorten a string to 10 chars if necessary:

trimComplete(): this is a nice trim function which removes all non-printable characters from the end and the beginning of the string. You know sometimes there are weird characters which we dont see and they are still here and cause problems when working with the strings.

defuseHTML(): hey guys I love this name. i have to patent it every string you pass into it will be defused of being HTML. this is usefull especially when you want to display some HTML within an application.

ensureSlash(): oh this one is very useful when you deal a lot with virtual paths (luckily within a web application you do) and want to be sure that there is a slash at the end. so lets say you have some config var which holds some path and you build the filename with this path then you could use this method to ensure that there is a slash even if the user havent entered one. Example:

URL(): as an URL is some kind of a special string it fits in here to build an URL with this given function. It makes the code nicer and the parameters are automatically URLEncoded. Example use for a redirect:

toInt(), toFloat(): those both take a string as a parameter and convert it into a numeric value (integer/float). If the conversion is not possible an alternative value is passed through. Very nice to use for e.g. Query string parameters or other user input fields. Example which tries to parse an ID parameter from the querystring as a number. If it cannot be parsed then 0 is passed through (with this youre always sure to deal with a number):

captialize(): last but not least another useful function which is very common in a lot of apps out there. It converts the first char of each word found in a string to upper case. Useful for names of persons.

There are some more in the class which are very useful. This class has been used for years now and so there should not be any bugs (or at least not known yet ). Feel free to suggest some other useful stuff which could fit in here so I could expand it. Have fun. Here are the downloads:

thx craig … i am just a bit concerned about performance because this function is used very often in a lot of apps and the change compared to the previous one is dramatic.
i am just doing some brainstorming if there would be maybe other solutions as well .. you know some function like this is called a lot.

i was thinking of something like:
1. looping through all given placeholders
2. replacing its occurencies and surrounding it with some special char. the special char would prevent the repeated replacement (if regex is done correctly)
3. remove all the special chars.

havent tried yet if it would work .. what do you think? if yes, what char could we use?

Ok, but it’s premature optimization without data, so here is some:http://pastebin.com/m34fb4206
It’s a csv, so you can download and import into Excel/Calc/whatever.

I already identified the most expensive operation, creating the RegExp object. After moving the RegExp out of the method scope and into a global, the new method actually performs better in most cases, and the same in one case.

Eh, forget about the previous data (lies, damn lies, and statistics). I had a bug in the performance code where the test string was modified making the tests run faster. Here is the new code: http://pastebin.com/m3a71221e

In hindsite, the tests only showed that the methods both perform linearly on the same length of args array and same number of replacements. I’ve varied the number of the replacements in the new code.

I found that both methods performed linearly based on the number of replacements made. The average time (on my development box) per a replacement was 0.0015 ms for the original method and 0.0362 ms for the new method. Since this it was O(n) and the difference is a fraction of a millisecond, I’ve decided to run with it for our code base

thanks craig for your samples and your ideas … this made me playing around today for some time
yeah the regex object creation takes the most time .. but i discovered that the main problem with your approach is that its execution lasts longer when the placeholders are used more than once in a string. example:

the only little odd on this is that in theory the string could contain a sequence of chars which is used internally for escaping. this results in an error then .. but the chance is very very low .. take a look at the code.
Thanks for your inspiration craig and your time! the updated version can be found in the ajaxed SVN as this file here is not maintained.