pcase.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
txtInput.Text = "The quick brown fox jumps over the lazy dog."
btnSubmit_OnClick(Nothing, Nothing)
End If
End Sub
Sub btnSubmit_OnClick(sender As Object, e As EventArgs)
Dim strTemp As String
strTemp = txtInput.Text
litOriginal.Text = strTemp
litLCase.Text = LCase(strTemp)
litUCase.Text = UCase(strTemp)
litPCase.Text = PCase(strTemp)
' Im still using the old PCase function we wrote just for consistency.
' It's really no longer needed because now that you can use real VB
' and not just VBScript, the following works just fine:
litStrConv.Text = StrConv(strTemp, vbProperCase)
End Sub
' This function takes a string and converts to Proper Case.
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
Loop
' 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
</script>
<html>
<head>
<title>ASP.NET Proper Case Sample</title>
</head>
<body>
<table border="1" cellpadding="4" cellspacing="2">
<tr>
<td align="right"><strong>Original String:</strong></td>
<td><asp:Literal id="litOriginal" runat="server" /></td>
</tr>
<tr>
<td align="right"><strong>LCase (VBScript) String:</strong></td>
<td><asp:Literal id="litLCase" runat="server" /></td>
</tr>
<tr>
<td align="right"><strong>UCase (VBScript) String:</strong></td>
<td><asp:Literal id="litUCase" runat="server" /></td>
</tr>
<tr>
<td align="right"><strong>PCase (ASP 101) String:</strong></td>
<td><asp:Literal id="litPCase" runat="server" /></td>
</tr>
<tr>
<td align="right"><strong>StrConv(vbProperCase) (VB) String:</strong></td>
<td><asp:Literal id="litStrConv" runat="server" /></td>
</tr>
</table>
<form runat="server">
Try it out on your input:<br />
<asp:TextBox id="txtInput" size="50" runat="server" /><br />
<asp:Button id="btnSubmit" runat="server"
OnClick="btnSubmit_OnClick"
Text="Convert Text"
/>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/pcase_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>