Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleConvert text into proper case
KeywordsTextBox, case, proper case, propercase, StrConv
CategoriesControls, Utilities
 
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 ProperCase function.

The ProperCase function looks through the string seeing which characters are letters and which are not. It capitalizes the first letter after any non-letter.

 
' Convert the text into proper case.
Function ProperCase(ByVal txt As String) As String
Dim txtlen As Long
Dim need_cap As Boolean
Dim i As Integer
Dim ch As String

    txt = LCase(txt)
    txtlen = Len(txt)
    need_cap = True
    For i = 1 To txtlen
        ch = Mid$(txt, i, 1)
        If ch >= "a" And ch <= "z" Then
            If need_cap Then
                Mid$(txt, i, 1) = UCase$(ch)
                need_cap = False
            End If
        Else
            need_cap = True
        End If
    Next i
    ProperCase = txt
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-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated