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

No comments: