ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





Script Output:
Original String: The quick brown fox jumps over the lazy dog.
LCase (VBScript) String: the quick brown fox jumps over the lazy dog.
UCase (VBScript) String: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
PCase (ASP 101) String: The Quick Brown Fox Jumps Over The Lazy Dog.
Try it out on your input:


ASP HTML Download            FAQ

ASP.NET Version

Ever wonder why there are built in functions to make stuff lower case and upper case, but not one to put something into proper case? Well we did! It's built into Visual Basic using the StrConv(string, vbProperCase) command so why can't we use it in VBScript? Well now you can.

I've run into this quite a few times and for the most part it's not a big problem. Each time I need to do it I usually use a quick hack like:

UCase(Left(strInput, 1)) & LCase(Mid(strInput, 2))

to handle it, but I've probably written it a dozen times and it gets annoying. Finally, one of our visitors got tired of doing this over and over, decided that enough is enough, and spent the time to write a function to help automate this task. Luckily for you, Brian Shamblen from Certified Capital Corporation is not only the type of person who tries to make his own coding easier, he also likes to share! I received a version of this function from Brian who was visiting the site and felt that he'd like to contribute it in the hopes that others would benefit from his work. I've "hacked and slashed" it a little and added some comments to make it seem more like an official ASP 101 sample, but the motivation and real credit goes to someone probably very much like you! You see... we actually do listen to our users once in a while! Thanks Brian!

When I recieved it, it was actually named TCase for title case. I've changed it to PCase for proper case to match it to the term that Visual Basic uses for this type of string. There are actually a few issues with the script. The main issue is: what to do with things you want to keep capitalized or those you don't? For example, I'd assume you'd want McCarthy to stay McCarthy and not change to Mccarthy. Well it doesn't. Similarly "a", "an", and "the" get changed to "A", "An", and "The" even though you wouldn't want this in a title. As far as I know, there's not easy way to handle this type of thing in a dozen line script, so we simply don't! I replicates the functionality of VB's StrConv(string, vbProperCase). If you want any more than that, I'm sorry but you'll need to look elsewhere.

Alternate Version of PCase

One of our visitors sent in an alternate version of the VBScript PCase function we published in this sample. While the original version accomplished its task using all string-based functions, his version splits the phrase into an array of words and then loops through the array capitalizing each word. It just goes to show that no matter how simple a problem is, there's almost always more than one solution.

Rather then list the code here or create a zip file, I've simply added a new function named "PCaseAlternate" to the original script. So if you're interested in the alternate version of the code just take another look at the ASP source code.

Yet Another Version of PCase

As often happens, a visitor has taken one of our scripts and improved upon it. Here's the email which accompanied the script:

Hi,

I came across your site looking for an ASP soloution to the lack of PCase function problem. I noticed that you had 2 soloutions, but in the end I wrote my own.

My version breaks the string down into single chars, and makes a note of the previous char so that it can handle not just spaces, but punctuation too! This has the ability to correctly PCase names such as "Peter O'Tool" or "Camilla Parker-Bowles", although sadly it still cant deal with "McCartney" properly.

I haven't commented the code as it's fairly simple to understand, but you may wish to comment it yourself (or use better variable names) if you wish to display it on your site - for which you have my permission.

I didn't bother adding comments, but I did rename most of the variables to clean things up a little. This script is a little different from the other two in that it goes character by character, but that's what gives it the additional flexibility.

    Function PCase(ByVal strInput)' As String
        Dim I 'As Integer
        Dim CurrentChar, PrevChar 'As Char
        Dim strOutput 'As String

        PrevChar = ""
        strOutput = ""

        For I = 1 To Len(strInput)
            CurrentChar = Mid(strInput, I, 1)

            Select Case PrevChar
                Case "", " ", ".", "-", ",", """", "'"
                    strOutput = strOutput & UCase(CurrentChar)
                Case Else
                    strOutput = strOutput & LCase(CurrentChar)
            End Select

            PrevChar = CurrentChar
        Next 'I

        PCase = strOutput
    End Function


Samples

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers