Library code snippets

Proper Case Function

This neat function capitalizes the first characters of each word setting, and turns all other characters to lowercase.

Public Function ProperCase(strText As String) As String 'Capitalize all first characters of the input string 'and set all the others to lowercase
    On Error GoTo Err_ProperCase
   
    Dim I As Integer, intLen As Integer, strTemp As String, strFinal As String
    Dim isSpace As Boolean
    strTemp = LCase(Trim(strText))
    intLen = Len(Trim(strText))
    strFinal = String(intLen, " ")
    isSpace = True
    For I = 1 To intLen
        If Mid(strTemp, I, 1) = Chr(32) Then
            strFinal = Mid(strFinal, 1, I - 1) & Chr(32) & Mid(strFinal, I + 1)
            isSpace = True
        ElseIf isSpace Then
            strFinal = Mid(strFinal, 1, I - 1) & UCase(Mid(strTemp, I, 1)) & Mid(strFinal, I + 1)
            isSpace = False
        Else
            strFinal = Mid(strFinal, 1, I - 1) & Mid(strTemp, I, 1) & Mid(strFinal, I + 1)
        End If
    Next I
    ProperCase = strFinal
   
Exit_ProperCase:
    Exit Function
   
Err_ProperCase:
    MsgBox Err.Description & vbCrLf & strText, vbInformation, "ProperCase function"
End Function

Comments

  1. 15 Jun 2009 at 14:09

    .net equivalent:

    System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase()

  2. 09 Aug 2007 at 16:12
    A bit simpler... INITCAP
  3. 07 Jul 2007 at 22:32

    Simplest One.

    Text1.Text = StrConv(Text1.Text, vbProperCase)

  4. 18 Feb 2007 at 04:38

    Private Function FirstLetterCaps(str As String) As String
    Dim ExtractWord, NewStr As String
    Dim position As Integer
    position = InStr(str, " ")
    While position
        ExtractWord = LCase$(Left$(str, position))
        NewStr = NewStr & UCase$(Left$(ExtractWord, 1)) & Mid$(ExtractWord, 2)
        str = Right$(str, Len(str) - position)
        position = InStr(str, " ")
    Wend
        ExtractWord = LCase$(str)
        NewStr = NewStr & UCase$(Left$(ExtractWord, 1)) & Mid$(ExtractWord, 2)
        FirstLetterCaps = NewStr
    End Function












    Above function is called wth str argument passed to it. First word of the str is extracted by using Left$ function and changed to LCase for sure. Then First char of the extracted word is converted to UCase while remaining are retained as Lcase, and this word is added to Newstr. The str is then deducted of the first word using Right$ function. Position is moved to next space location. As the loop so performed, the Newstr continuously accumulates words while str looses them until str is left with last word whose first char is finally converted to UCase and added to Newstr. The Newstr thus is formed with  First Chars UCase which is then returned to calling procedure.

    Note: With VB6, StrConv(string, vbProperCase)  function performs the same task in a single line of code with effortless ease.

  5. 17 Dec 2002 at 12:54
    The code worked well on an ASP 3.0 project after some modifications.  One enhancement I added was checks for apostrophes within names (like O'Neal), and for apostrophe 's' at the end of a sentence.  Thanks!
  6. 09 Oct 2001 at 10:37
    StrConv(string, vbProperCase) does this in VB 6.  Otherwise it's handy for versions 4/5.

    D
  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Proper Case Function.

Leave a comment

Sign in or Join us (it's free).

 hobbes68

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!