Posted By

Scooter on 05/19/08


Tagged

strings


Versions (?)

Proper Case


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/04/proper-case.html

Replica of the PCase() function from Visual Basic for VBScript. Slightly different in that it supports Scottish and Irish names like MacDonald and O'Brien.

  1. <%
  2. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Format a string in proper case.
  10. function PCase(someString)
  11. dim position
  12. dim space
  13. dim result
  14. dim regEx
  15.  
  16. position = 1
  17. set regEx = new RegExp
  18. regEx.Pattern = "^(Mc[A-Z]{1}[A-Za-z]|O\'[A-Z]{1}[A-Za-z]|Mac[A-Z]{1}[A-Za-z])"
  19.  
  20. ' Loop through the string checking for spaces.
  21. do while InStr(position, someString, " ", 1) <> 0
  22. ' Find the position of the next space.
  23. space = InStr(position, someString, " ", 1)
  24.  
  25. ' Capitalize (and append to our output) the first character after the space which was handled by the previous run through the loop.
  26. result = result & UCase(Mid(someString, position, 1))
  27.  
  28. ' Check for situations like McDonald or O'Brien.
  29. if not regEx.Test(Mid(someString, position, space - position)) then
  30. ' Lowercase (and append to our output) the rest of the string up to and including the current space.
  31. result = result & LCase(Mid(someString, position + 1, space - position))
  32. else
  33. if Left(Mid(someString, position), 3) = "Mac" then
  34. ' Leave the next three characters intact.
  35. result = result & Mid(someString, position + 1, 3)
  36. ' Append the rest of the string.
  37. result = result & LCase(Mid(someString, position + 4, space - position + 4))
  38. else
  39. ' Leave the next two characters intact.
  40. result = result & Mid(someString, position + 1, 2)
  41. ' Append the rest of the string.
  42. result = result & LCase(Mid(someString, position + 3, space - position + 3))
  43. end if
  44. end if
  45.  
  46. position = space + 1
  47. loop
  48.  
  49. ' Capitalize the first character of the last word after the final space (or the only word if there were no spaces).
  50. result = result & UCase(Mid(someString, position, 1))
  51.  
  52. ' Check for situations like McDonald or O'Brien.
  53. if not regEx.Test(Mid(someString, position)) then
  54. ' Lowercase (and append to our output) the rest of the string up to and including the current space.
  55. result = result & LCase(Mid(someString, position + 1))
  56. else
  57. if Left(Mid(someString, position), 3) = "Mac" then
  58. ' Leave the next three characters intact.
  59. result = result & Mid(someString, position + 1, 3)
  60. ' Append the rest of the string.
  61. result = result & LCase(Mid(someString, position + 4))
  62. else
  63. ' Leave the next two characters intact.
  64. result = result & Mid(someString, position + 1, 2)
  65. ' Append the rest of the string.
  66. result = result & LCase(Mid(someString, position + 3))
  67. end if
  68. end if
  69.  
  70. set regEx = Nothing
  71. PCase = result
  72. end function
  73. %>

Report this snippet  

You need to login to post a comment.