Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

Saturday, October 18, 2008

Levenshtein distance

The Levenshtein distance between two strings is a measurement of similarity. The smaller the distance, the more similar two strings are. Our PHP programmer friends have a function to calculate this distance; we deserve one too.


ASP

  1. function levenshtein(byVal first, byVal second)
  2.     dim distance
  3.     dim truncateLength
  4.     if first = second then
  5.         ' The distance is zero if the strings are identical.
  6.         distance = 0

  7.     else
  8.         ' The distance is at least the difference of the lengths of the two strings.
  9.         distance = abs(len(first) - len(second))
  10.         ' Force the strings to be the same length to prevent overflows.
  11.         truncateLength = ((len(first) + len(second)) - distance) / 2
  12.         first = Left(first, truncateLength)
  13.         second = Left(second, truncateLength)
  14.         ' Compare the corresponding characters in each string.
  15.         for i = 1 to truncateLength
  16.             if Mid(first, i, 1) <> Mid(second, i, 1) then
  17.                 distance = distance + 1
  18.             end if
  19.         next
  20.     end if
  21.     levenshtein = distance
  22. end function

View ASP implementation on Snipplr

Saturday, October 11, 2008

WordCount

PHP has a function called str_word_count() which allows you to count the number of words in a string, and an array or associative array of those words. Unfortunately for us, ASP doesn't support optional parameters, so we can't duplicate all of this functionality in a single function, so we'll just concentrate on the main feature of counting the number of words.


ASP

  1. function WordCount(byVal someString)
  2.     dim position
  3.     dim spaces
  4.     spaces = 1
  5.     someString = trim(someString)
  6.     for position = 1 to len(someString)
  7.         if mid(someString, position, 1) = " " and not mid(someString, position - 1, 1) = " " then
  8.             spaces = spaces + 1
  9.         end if
  10.     next
  11.     WordCount = spaces
  12. end function

View ASP implementation on Snipplr

Saturday, September 13, 2008

str_repeat

This week we're duplicating another string function from PHP: str_repeat().


ASP

  1. function str_repeat(input, multiplier)
  2.     dim output
  3.     output = ""
  4.     for i = 1 to multiplier
  5.         output = output & input
  6.     next
  7.     str_repeat = output
  8. end function

View ASP implementation on Snipplr

Saturday, September 6, 2008

str_pad

PHP has a very useful function called str_pad(). It allows you to ensure that a string will conform to a specific length by adding characters of your choice on the end of your choice. A good practical example is when you want a number with leading zeros. ASP doesn't have an equivalent function out of the box, but we're going to write one today; as usual, we'll follow the same syntax to make it easy for those who are already familiar with PHP.


ASP

  1. Const STR_PAD_LEFT = "LEFT"
  2. Const STR_PAD_RIGHT = "RIGHT"
  3. Const STR_PAD_BOTH = "BOTH"
  4. ' Pad a string to a certain length with another string.
  5. function str_pad(input, pad_length, pad_string, pad_type)
  6.     dim output
  7.     dim difference
  8.     output = ""
  9.     difference = pad_length - len(input)
  10.     if difference > 0 then
  11.         select case ucase(pad_type)
  12.         case "LEFT"
  13.             for i = 1 to difference step len(pad_string)
  14.                 output = output & pad_string
  15.             next
  16.             output = right(output & input, pad_length)
  17.         case "BOTH"
  18.             output = input
  19.             for i = 1 to difference step len(pad_string) * 2
  20.                 output = pad_string & output & pad_string
  21.             next
  22.             ' Not sure if it will step far enough when difference is an odd number, so this next block is just in case.
  23.             if len(output) < pad_length then
  24.                 output = output & pad_string
  25.             end if
  26.             output = left(output, pad_length)
  27.         case else
  28.             for i = 1 to difference step len(pad_string)
  29.                 output = output & pad_string
  30.             next
  31.             output = left(input & output, pad_length)
  32.         end select
  33.     else
  34.         output = input
  35.     end if
  36.     str_pad = output
  37. end function

Thinking back to my days as a web developer, this was probably the most reused function out of all I'd ever written.


View ASP implementation on Snipplr

Saturday, April 19, 2008

Proper Case

Both ASP and PHP have handy functions for forcing characters in a string to be all uppercase or all lowercase, but what about proper case? Proper case is used for names of people, titles of songs or movies, etc. The first letter in each word is capitalized, and the other letters are lowercase.


PHP has a built-in function for this, but the usage is slightly different and the name of the function is inconsistent, so we're going to write a facade function.


PHP

  1. function strtoproper($someString)
  2. {
  3.     return ucwords(strtolower($someString));
  4. }

ASP doesn't have a function for this, though full-blown Visual Basic does. We're going to duplicate that functionality, and make a few improvements. Consider names like McDonald and O'Brien. An ordinary proper case function would not handle these properly. We're going to use a regular expression to find these situations and handle them appropriately.


ASP

  1. function PCase(someString)
  2.     dim position
  3.     dim space
  4.     dim result
  5.     dim regEx
  6.     position = 1
  7.     set regEx = new RegExp
  8.     regEx.Pattern = "^(Mc[A-Z]{1}[A-Za-z]|O\'[A-Z]{1}[A-Za-z]|Mac[A-Z]{1}[A-Za-z])"
  9.     ' Loop through the string checking for spaces.
  10.     do while InStr(position, someString, " ", 1) <> 0
  11.         ' Find the position of the next space.
  12.         space = InStr(position, someString, " ", 1)
  13.         ' Capitalize (and append to our output) the first character after the space which was handled by the previous run through the loop.
  14.         result = result & UCase(Mid(someString, position, 1))
  15.         ' Check for situations like McDonald or O'Brien.
  16.         if not regEx.Test(Mid(someString, position, space - position)) then
  17.             ' Lowercase (and append to our output) the rest of the string up to and including the current space.
  18.             result = result & LCase(Mid(someString, position + 1, space - position))
  19.         else
  20.             if Left(Mid(someString, position), 3) = "Mac" then
  21.                 ' Leave the next three characters intact.
  22.                 result = result & Mid(someString, position + 1, 3)
  23.                 ' Append the rest of the string.
  24.                 result = result & LCase(Mid(someString, position + 4, space - position + 4))
  25.             else
  26.                 ' Leave the next two characters intact.
  27.                 result = result & Mid(someString, position + 1, 2)
  28.                 ' Append the rest of the string.
  29.                 result = result & LCase(Mid(someString, position + 3, space - position + 3))
  30.             end if
  31.         end if
  32.         position = space + 1
  33.     loop
  34.     ' Capitalize the first character of the last word after the final space (or the only word if there were no spaces).
  35.     result = result & UCase(Mid(someString, position, 1))
  36.     ' Check for situations like McDonald or O'Brien.
  37.     if not regEx.Test(Mid(someString, position)) then
  38.         ' Lowercase (and append to our output) the rest of the string up to and including the current space.
  39.         result = result & LCase(Mid(someString, position + 1))
  40.     else
  41.         if Left(Mid(someString, position), 3) = "Mac" then
  42.             ' Leave the next three characters intact.
  43.             result = result & Mid(someString, position + 1, 3)
  44.             ' Append the rest of the string.
  45.             result = result & LCase(Mid(someString, position + 4))
  46.         else
  47.             ' Leave the next two characters intact.
  48.             result = result & Mid(someString, position + 1, 2)
  49.             ' Append the rest of the string.
  50.             result = result & LCase(Mid(someString, position + 3))
  51.         end if
  52.     end if
  53.     set regEx = Nothing
  54.     PCase = result
  55. end function

Wednesday, March 12, 2008

InstrCount()

This is a special mid-week edition of Reusable Code. While preparing this weekend's edition, I discovered that for the ASP examples we would need another utility function that is useful outside of working with Roman numerals. I feel it important enough to have it's own post, but I don't want to delay the upcoming posts.


ASP programmers are probably familiar with the Instr() function. It returns the location of one string within another. Sometimes you want to know how many times one string occurs within another. PHP programmers have a built-in function called substr_count() which can do this. We're going to write our own function in ASP to duplicate some of this functionality.


  1. function InstrCount(haystack, needle)
  2.     if needle <> "" then
  3.         InstrCount = UBound(Split(haystack, needle))
  4.     end if
  5. end function

It's fairly self-explanatory, but just in case you're not familiar with the Split() function... We are splitting the string into an array, using the search string (needle) as our delimiter. The upper bound of the array gives us the number of needles in our haystack.


Why didn't I call the function substr_count() to match PHP? Normally I do like to have things consistent between languages because it makes it easier for people to go from one to the other, but in this particular case, the PHP function substr_count has two optional arguments. Since ASP does not support optional arguments (at least not the way you would expect), I went with a name more similar to the complementary Instr() function.