/ 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.
Expand |
Embed | Plain Text
<% ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved. ' ' This work is licensed under the Creative Commons Attribution License. To view ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California ' 94305, USA. ' Format a string in proper case. function PCase(someString) dim position dim space dim result dim regEx position = 1 set regEx = new RegExp regEx.Pattern = "^(Mc[A-Z]{1}[A-Za-z]|O\'[A-Z]{1}[A-Za-z]|Mac[A-Z]{1}[A-Za-z])" ' Loop through the string checking for spaces. do while InStr(position, someString, " ", 1) <> 0 ' Find the position of the next space. space = InStr(position, someString, " ", 1) ' Capitalize (and append to our output) the first character after the space which was handled by the previous run through the loop. result = result & UCase(Mid(someString, position, 1)) ' Check for situations like McDonald or O'Brien. if not regEx.Test(Mid(someString, position, space - position)) then ' Lowercase (and append to our output) the rest of the string up to and including the current space. result = result & LCase(Mid(someString, position + 1, space - position)) else if Left(Mid(someString, position), 3) = "Mac" then ' Leave the next three characters intact. result = result & Mid(someString, position + 1, 3) ' Append the rest of the string. result = result & LCase(Mid(someString, position + 4, space - position + 4)) else ' Leave the next two characters intact. result = result & Mid(someString, position + 1, 2) ' Append the rest of the string. result = result & LCase(Mid(someString, position + 3, space - position + 3)) end if end if position = space + 1 loop ' Capitalize the first character of the last word after the final space (or the only word if there were no spaces). result = result & UCase(Mid(someString, position, 1)) ' Check for situations like McDonald or O'Brien. if not regEx.Test(Mid(someString, position)) then ' Lowercase (and append to our output) the rest of the string up to and including the current space. result = result & LCase(Mid(someString, position + 1)) else if Left(Mid(someString, position), 3) = "Mac" then ' Leave the next three characters intact. result = result & Mid(someString, position + 1, 3) ' Append the rest of the string. result = result & LCase(Mid(someString, position + 4)) else ' Leave the next two characters intact. result = result & Mid(someString, position + 1, 2) ' Append the rest of the string. result = result & LCase(Mid(someString, position + 3)) end if end if set regEx = Nothing PCase = result end function %>
You need to login to post a comment.
