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:
<%
Function ProperCase(sString)
Dim sWhiteSpace, bCap, iCharPos, sChar
sWhiteSpace = Chr(32) & Chr(9) & Chr(13)
sString = LCase(sString)
bCap = True
For iCharPos = 1 to Len(sString)
sChar = Mid(sString, iCharPos, 1)
If bCap = True Then
sChar = UCase(sChar)
End If
ProperCase = ProperCase + sChar
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:
- Words like bill gates iii will become Bill Gates Iii - notice the last two i's not
uppercased
- 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) &
"'"
|