Proper capitalization/case routine

Results 1 to 4 of 4

Thread: Proper capitalization/case routine

  1. #1
    Bill Anthony Guest

    Default Proper capitalization/case routine

    Greetings everyone.<BR><BR>I have seen a few examples of code, whereby, you can give capitalization or "proper case" to an input string from a submited form. I have tried to implement the examples I have found, but have had no luck. Here are the examples I tried to use:<BR><BR>EXAMPLE 01<BR>============================================ =======<BR>Function Capitalize(str)<BR> &#039This function capitalizes each of the words in a sentence.<BR><BR> Dim t<BR> t = str<BR><BR> If t &#060;&#062; "" Then<BR> Mid(t, 1, 1) = UCase(Mid(t, 1, 1))<BR> For i = 1 To Len(t) - 1<BR> If Mid(t, i, 2) = Chr(13) + Chr(10) Then<BR> &#039 Capitalize words preceded by carriage return plus<BR> &#039 linefeed combination.<BR> Mid(t, i + 2, 1) = UCase(Mid(t, i + 2, 1))<BR> End If<BR><BR> If Mid(t, i, 1) = " " Then<BR> &#039 Capitalize words preceded by a space:<BR> Mid(t, i + 1, 1) = UCase(Mid(t, i + 1, 1))<BR> End If<BR> Next<BR> End If<BR><BR> Capitalize = t<BR>End Function<BR><BR><BR>EXAMPLE 02<BR>============================================ =======<BR>Function PCase(strInput)<BR> Dim iPosition &#039 Our current position in the string (First character = 1)<BR> Dim iSpace &#039 The position of the next space after our iPosition<BR> Dim strOutput &#039 Our temporary string used to build the function&#039s output<BR><BR> &#039 Set our position variable to the start of the string.<BR> iPosition = 1<BR> <BR> &#039 We loop through the string checking for spaces.<BR> &#039 If there are unhandled spaces left, we handle them...<BR> Do While InStr(iPosition, strInput, " ", 1) &#060;&#062; 0<BR> &#039 To begin with, we find the position of the offending space.<BR> iSpace = InStr(iPosition, strInput, " ", 1)<BR> <BR> &#039 We uppercase (and append to our output) the first character after<BR> &#039 the space which was handled by the previous run through the loop.<BR> strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))<BR> <BR> &#039 We lowercase (and append to our output) the rest of the string<BR> &#039 up to and including the current space.<BR> strOutput = strOutput & LCase(Mid(strInput, iPosition + 1, iSpace - iPosition))<BR><BR> &#039 Note:<BR> &#039 The above line is something you may wish to change to not convert<BR> &#039 everything to lowercase. Currently things like "McCarthy" end up<BR> &#039 as "Mccarthy", but if you do change it, it won&#039t fix things like<BR> &#039 ALL CAPS. I don&#039t see an easy compromise so I simply did it the<BR> &#039 way I&#039d expect it to work and the way the VB command<BR> &#039 StrConv(string, vbProperCase) works. Any other functionality is<BR> &#039 left "as an exercise for the reader!"<BR> <BR> &#039 Set our location to start looking for spaces to the<BR> &#039 position immediately after the last space.<BR> iPosition = iSpace + 1<BR> Loop<BR><BR> &#039 Because we loop until there are no more spaces, it leaves us<BR> &#039 with the last word uncapitalized so we handle that here.<BR> &#039 This also takes care of capitalizing single word strings.<BR> &#039 It&#039s the same as the two lines inside the loop except the<BR> &#039 second line LCase&#039s to the end and not to the next space.<BR> strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))<BR> strOutput = strOutput & LCase(Mid(strInput, iPosition + 1))<BR><BR> &#039 That&#039s it - Set our return value and exit<BR> PCase = strOutput<BR>End Function<BR>%&#062;<BR><BR><BR>&#060;%&#039 Run-time Code<BR>Dim strTemp &#039 The string we play with<BR><BR>&#039 Get the string from the form<BR>strTemp = CStr(Request.Form("inputstring"))<BR><BR>&#039 If it&#039s empty, use our default<BR>If strTemp = "" Then strTemp = "The quick brown fox jumps over the lazy dog."<BR><BR>&#039 Output table of original, LCase, UCase, and PCase&#039d strings<BR>&#039 then show form if they want to try it on their own string.<BR>%&#062;<BR>&#060;TABLE BORDER="1" CELLPADDING="4" CELLSPACING="2"&#062;<BR> &#060;TR&#062;<BR> &#060;TD ALIGN="right"&#062;&#060;B&#062;Original String:&#060;/B&#062;&#060;/TD&#062;<BR> &#060;TD&#062;&#060;%= strTemp %&#062;&#060;/TD&#062;<BR> &#060;/TR&#062;<BR> &#060;TR&#062;<BR> &#060;TD ALIGN="right"&#062;&#060;B&#062;LCase (VBScript) String:&#060;/B&#062;&#060;/TD&#062;<BR> &#060;TD&#062;&#060;%= LCase(strTemp) %&#062;&#060;/TD&#062;<BR> &#060;/TR&#062;<BR> &#060;TR&#062;<BR> &#060;TD ALIGN="right"&#062;&#060;B&#062;UCase (VBScript) String:&#060;/B&#062;&#060;/TD&#062;<BR> &#060;TD&#062;&#060;%= UCase(strTemp) %&#062;&#060;/TD&#062;<BR> &#060;/TR&#062;<BR> &#060;TR&#062;<BR> &#060;TD ALIGN="right"&#062;&#060;B&#062;PCase (ASP 101) String:&#060;/B&#062;&#060;/TD&#062;<BR> &#060;TD&#062;&#060;%= PCase(strTemp) %&#062;&#060;/TD&#062;<BR> &#060;/TR&#062;<BR>&#060;/TABLE&#062;<BR><BR>&#060;FORM ACTION="&#060;%= Request.ServerVariables("SCRIPT_NAME") %&#062;" METHOD="post"&#062;<BR> Try it out on your input:<BR><BR> &#060;INPUT TYPE="text" NAME="inputstring" SIZE="50"&#062;&#060;/INPUT&#062;<BR><BR> &#060;INPUT TYPE="submit"&#062;&#060;/INPUT&#062;<BR><BR>&#060;/FORM&#062;<BR><BR><BR>END<BR>===================== ================================<BR><BR>Neither seems to work. In the first example, I receive an error referencing the "Mid" function. The second example doesn&#039t return any errors; yet it doesn&#039t capitalize the string either.<BR><BR>Is there a caps function that actually "works" ? What other methods, easier or otherwise, that can be used? Pleasse let me know. Thanks in advance!<BR><BR>Bill Anthony<BR>Ellington, CT

  2. #2
    P. Kiez Guest

    Default RE: Proper capitalization/case routine

    I&#039ve used the ASP101 version and it works fine for me... Are you sure you&#039re using VBScript as your language (as opposed to JScript or PerlScript)? Wish I could be more help, but I don&#039t think your problem is quite where you think it is.

  3. #3
    Bill Anthony Guest

    Default RE: Proper capitalization/case routine

    Yes. Definitely. I have the "&#060;%@ LANGUAGE = VBSCRIPT %&#062;" statement at the top of all my pages and the context of all my scripts are based on the VBScript language.

  4. #4
    Douglas L. Setzer, II Guest

    Default RE: Proper capitalization/case routine

    Here is the function that I use to fix capitalization. It doesn&#039t take into account carriage returns, though.<BR><BR>The usage for this is:<BR>strTemp = UpCaps("this is the string to capitalize.")<BR><BR>If you then view strTemp it will equal "This Is The String To Capitalize."<BR><BR>-Doug<BR><BR>CODE:<BR>Function UpCaps(UseString)<BR> Dim intCnt, intTemp<BR> Dim blnCap: blnCap = True<BR> <BR> For intCnt = 1 To Len(UseString)<BR> intTemp = Asc(Mid(UseString, intCnt, 1)) <BR> If (intTemp &#062;= 65 And intTemp &#060;= 90) Or (intTemp &#062;= 97 And intTemp &#060;= 122) Then<BR> If blnCap = True Then<BR> UseString = Left(UseString, intCnt - 1) & Ucase(Mid(UseString, intCnt, 1)) & Right(UseString, len(UseString)-intCnt)<BR> &#039Mid(UseString, intCnt, 1) = UCase(Mid(UseString, intCnt, 1))<BR> blnCap = False<BR> End If<BR> Else<BR> blnCap = True<BR> End If<BR> Next<BR><BR> UpCaps = UseString<BR>End Function


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •