Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
500MB 27GB Web Hosting - $9.95/Month
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleConvert text into proper case, version 2
KeywordsTextBox, case, proper case, propercase, StrConv
CategoriesControls, Utilities
 
By WJK.

Visual Basic's StrConv function can capitalize the first letter in every word in a string, but it doesn't handle special values like "O'Conner" and "I.B.M" correctly. This program demonstrates StrConv and a custom LwrToUpprCase function.

The LwrToUpprCase function looks through the string seeing which characters are letters and which are not. It capitalizes those that satisfy the rules shown in the comment at the beginning of the function.

 
' subroutine to normalize 1 piece of data Capitalize
'           Inputs:  char label                            
' 10/29/91
'          Outputs:  Normalized Label
'
' Rules Cap char after [
' ][>][-][.][/][#][0-9][&][Mc][,][(],[\],[|]
'       Cap char after [!][@][$][%][^][*][+][=]["]['(not s)]
'       abbreviations [po][nw][ne][sw][se][ii] are
' capitalized
'       abbreviations [st][th][nd][rd][c/o]    are not
' capitalized
'               words [of][and]                are not
' capitalized
Public Function LwrToUpprCase(ByVal edit$) As String
    Dim A$, b$, c$, I%
    On Error Resume Next

    If Len(Trim(edit$)) = 0 Then
        LwrToUpprCase = edit$
        Exit Function
    End If

    edit$ = edit$ + Space$(3)       'end of word match ???
    Mid$(edit$, 1, 1) = UCase$(Mid$(edit$, 1, 1)) 'cap
        ' first char
    If Mid$(edit$, 1, 3) = "Po " Then
        Mid$(edit$, 1, 3) = "PO " 'PO Box cap
    End If
    b$ = "NwNeSwSeIi"       'NW NE SW SE II
    If Mid$(edit$, 1, 2) = "Mc" Then
        Mid$(edit$, 3, 1) = UCase$(Mid$(edit$, 3, 1))
    End If
    If Mid$(edit$, 1, 1) > Chr$(47) And Mid$(edit$, 1, 1) < _
        Chr$(58) Then
        Mid$(edit$, 2, 1) = UCase$(Mid$(edit$, 2, 1)) 'nos
            ' 0 - 9
    End If
    c$ = " >-./#&,(!@$%^*+=\|" & Chr(34)
    If InStr(c$, Mid$(edit$, 1, 1)) > 0 Then
        Mid$(edit$, 2, 1) = UCase$(Mid$(edit$, 2, 1))
    End If
    For I% = 2 To Len(edit$)
        If InStr(b$, Mid$(edit$, I%, 2)) > 0 Then
            If Mid$(edit$, I% - 1, 1) = " " And Mid$(edit$, _
                I% + 2, 1) = " " Then
                Mid$(edit$, I%, 2) = UCase$(Mid$(edit$, I%, _
                    2))
            End If
            I% = I% + 2
        End If
        If Mid$(edit$, I%, 4) = " of " Then 'dont cap word
            ' "of"
            I% = I% + 2
            GoTo skipcapchar
        ElseIf Mid$(edit$, I%, 5) = " and " Then 'dont cap
            ' word "and"
            I% = I% + 3
            GoTo skipcapchar
        
        ElseIf Mid$(edit$, I%, 5) = " c/o " Then 'dont cap
            ' abbrev c/o
            I% = I% + 3
            GoTo skipcapchar
        End If
        If InStr(c$, Mid$(edit$, I%, 1)) > 0 And I% < _
            Len(edit$) Then
            Mid$(edit$, I% + 1, 1) = UCase(Mid$(edit$, I% + _
                1, 1))
        End If
        If Mid$(edit$, I%, 1) = Chr$(39) And Mid$(edit$, I% _
            + 1, 1) <> "s" Then
            Mid$(edit$, I% + 1, 1) = UCase$(Mid$(edit$, I% _
                + 1, 1)) 'O'Malley's
        End If
        If Mid$(edit$, I%, 2) = "Mc" Then
            Mid$(edit$, I% + 2, 1) = UCase$(Mid$(edit$, I% _
                + 2, 1))
        End If
        If InStr("0123456789", Mid$(edit$, I%, 1)) > 0 Then _
            'nos 0-9
            A$ = Mid$(edit$, I% + 1, 2)
            If A$ <> "st" And A$ <> "th" And A$ <> "nd" And _
                A$ <> "rd" Then
                Mid$(edit$, I% + 1, 1) = UCase$(Mid$(edit$, _
                    I% + 1, 1)) 'no's 0-9
            End If
        End If
skipcapchar:
    Next I%
    edit$ = RTrim$(edit$) 'trim space added earlier

    LwrToUpprCase = edit$
End Function
 
This function may not handle all special cases correctly but it does a better job than StrConv for this type of example.
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated