Active Server Corner
Quick Site Search
What's Inside
Home
Activity
Books
Excerpts
In-Depth
CodeLibrary
Columns
ActiveTalk
COMSutra
Cornered!
My 3 Cents
Square ONE
Downloads
Events
FAQ's
Jobs
Search
Tools
Product Reviews
Tutorials
Site Info
About Us
Advertising Info
Contact Us
Privacy Policy
Terms of Use
Write for Us
  CodeLibrary @ Active Server Corner   The source for source code

Propercase strings

Manohar Kamath
February 1, 1999

Contains: ASP

Here is a utility function to "Proper Case" your strings. Imagine a string like "MaNohar kamath" in a user name that you need to convert to "Manohar Kamath" You can use properCase function for this. Good candidates for propercasing are Address, City and Name fields in a database. However, beware of those ever-growing acronyms!

<%
  Response.Write ProperCase ("100 west virgina avenue")
%>  

The function takes one argument - sString, of type string. An example usage of the function is as follows:

<%
'------------------------------------
' Author: Manohar Kamath
' Name: ProperCase
' Date created: Jan 18th, 1998
'------------------------------------

Function ProperCase(sString)
  ' Declare the variables
  Dim sWhiteSpace, bCap, iCharPos, sChar

  ' Valid white space characters. Add more as necessary
  sWhiteSpace = Chr(32) & Chr(9) & Chr(13)

  ' Convert the string to lowercase, to start with
  sString = LCase(sString)

  ' Initialize the caps flag to True
  bCap = True
 
  ' Loop through each of the characters in the string
  For iCharPos = 1 to Len(sString)
    ' Get one character at a time
    sChar = Mid(sString, iCharPos, 1)

    ' If caps flag is true, upper case the character
    If bCap = True Then
      sChar = UCase(sChar)
    End If

    ' Append to the final string
    ProperCase = ProperCase + sChar

    ' If the character is a white space, raise the caps flag
    If InStr(sWhiteSpace, sChar) Then
      bCap = True
    Else
      bCap = False
    End If
  Next
End Function %>

Note: This code works with many strings. However, there are exceptions:

  1. Words like bill gates iii will become Bill Gates Iii - notice the last two i's not uppercased
  2. Words like o'brien become O'brien - this can be solved by adding ' to the string of white space characters
    sWhiteSpace = Chr(32) & Chr(9) & Chr(13) & "'"





Copyright © 1997-2000 Active Server Corner. All rights reserved.

Sponsored by Coastline Web Hosting in Santa Barbara, California