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

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

ASP Source Code:
' This function takes a string and converts to Proper Case.
' Prototyped by: Brian Shamblen on 3/18/99
' This version by: Us... naturally!
Function PCase(strInput)
	Dim iPosition  ' Our current position in the string (First character = 1)
	Dim iSpace     ' The position of the next space after our iPosition
	Dim strOutput  ' Our temporary string used to build the function's output
	' Set our position variable to the start of the string.
	iPosition = 1
	' We loop through the string checking for spaces.
	' If there are unhandled spaces left, we handle them...
	Do While InStr(iPosition, strInput, " ", 1) <> 0
		' To begin with, we find the position of the offending space.
		iSpace = InStr(iPosition, strInput, " ", 1)
		' We uppercase (and append to our output) the first character after
		' the space which was handled by the previous run through the loop.
		strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))
		' We lowercase (and append to our output) the rest of the string
		' up to and including the current space.
		strOutput = strOutput & LCase(Mid(strInput, iPosition + 1, iSpace - iPosition))
		' Note:
		' The above line is something you may wish to change to not convert
		' everything to lowercase.  Currently things like "McCarthy" end up
		' as "Mccarthy", but if you do change it, it won't fix things like
		' ALL CAPS.  I don't see an easy compromise so I simply did it the
		' way I'd expect it to work and the way the VB command
		' StrConv(string, vbProperCase) works.  Any other functionality is
		' left "as an exercise for the reader!"
		' Set our location to start looking for spaces to the
		' position immediately after the last space.
		iPosition = iSpace + 1
	' Because we loop until there are no more spaces, it leaves us
	' with the last word uncapitalized so we handle that here.
	' This also takes care of capitalizing single word strings.
	' It's the same as the two lines inside the loop except the
	' second line LCase's to the end and not to the next space.
	strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))
	strOutput = strOutput & LCase(Mid(strInput, iPosition + 1))
	' That's it - Set our return value and exit
	PCase = strOutput
End Function
' An alternate version suggested by a visitor.  Short and
' sweet, but it involves using an array which some users
' may dislike.  They should both work equally well... feel
' free to use whichever you prefer.
Function PCaseAlternate(strInput)
	Dim arrWords  ' Array to hold our words
	Dim intWord   ' Looping var
	' Split entered phrase at each space character
	arrWords = Split(strInput, " ")
	' Loop through array capitalizing the first character of
	' each word and making the remainder of each word lower case.
	For intWord = LBound(arrWords) To UBound(arrWords)
		arrWords(intWord) = UCase(Left(arrWords(intWord), 1)) _
		                    & LCase(Mid(arrWords(intWord), 2))
	' Rebuild our phrase from the array.
	PCaseAlternate = Join(arrWords, " ")
End Function
<%' Run-time Code
Dim strTemp ' The string we play with
' Get the string from the form
strTemp = CStr(Request.Form("inputstring"))
' If it's empty, use our default
If strTemp = "" Then strTemp = "The quick brown fox jumps over the lazy dog."
' Output table of original, LCase, UCase, and PCase'd strings
' then show form if they want to try it on their own string.
<table border="1" cellpadding="4" cellspacing="2">
		<td align="right"><strong>Original String:</strong></td>
		<td><%= strTemp %></td>
		<td align="right"><strong>LCase (VBScript) String:</strong></td>
		<td><%= LCase(strTemp) %></td>
		<td align="right"><strong>UCase (VBScript) String:</strong></td>
		<td><%= UCase(strTemp) %></td>
		<td align="right"><strong>PCase (ASP 101) String:</strong></td>
		<td><%= PCase(strTemp) %></td>
<form action="<%= Request.ServerVariables("SCRIPT_NAME") %>" method="post">
	Try it out on your input:<br />
	<input type="text" name="inputstring" value="<%= strTemp %>" size="50" /><br />
	<input type="submit" />

Back the the Sample Output

Back to the Sample Index

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



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