Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone aware of a "true" title case function

Status
Not open for further replies.

BlackBearDude

Programmer
Jan 13, 2003
33
0
0
US
I am in need of a function to convert any string to true title case.

For example:

"the time of day" should be converted to "The Time of Day"

in addition, it should handle or allow for exceptions such as "PO Box", "NE, SW, NW, SE", "O'Brien, McCarthy", "EST, DST", etc...

Any help is appreciated. I stumbled onto a nice function a month or two ago, for Foxpro, but can't seem to find it. I currently have one written in VB but it is extremely slow.

Thanks
 
There is a VB function called StrConv which will convert your string to proper case.

Try
StrConv("hello world", vbProperCase)

This however will not do abbreviations like NE of EST, however you could add these custom requirments to your own function. e.g.

Function TrueTitleCase(strText As String) As String

'use strConv to do capitalise 1st letter of each word
TrueTitleCase = StrConv(strText, vbProperCase)

'append space
TrueTitleCase = TrueTitleCase & " "

'do specific list
TrueTitleCase = Replace(TrueTitleCase, " po ", " PO ", 1, , vbTextCompare)
TrueTitleCase = Replace(TrueTitleCase, " ne ", " NE ", 1, , vbTextCompare)
TrueTitleCase = Replace(TrueTitleCase, " nw ", " NW ", 1, , vbTextCompare)
TrueTitleCase = Replace(TrueTitleCase, " se ", " SE ", 1, , vbTextCompare)
TrueTitleCase = Replace(TrueTitleCase, " sw ", " SW ", 1, , vbTextCompare)
TrueTitleCase = Replace(TrueTitleCase, " est ", " EST ", 1, , vbTextCompare)

'remove space from end
TrueTitleCase = Mid(TrueTitleCase, 1, Len(TrueTitleCase) - 1)

End Function


I'm not sure how you would cope with "o'neil". Simply searching for "'" would not work as "Stephen's" would end up as "Stephen'S".

Hope this helps a little

Stephen
 
Hi

The O'Neill and McCarthy issues can be fixed with Regular Expressions. Try the following:

Code:
Function ReplacePattern(Strng, patrn)
    Dim tmpStr As String
    Dim regEx As New RegExp
    Dim Match, Matches
    Set regEx = New RegExp              ' Create a regular expression.
    
    regEx.pattern = patrn               ' Set pattern.
    regEx.IgnoreCase = True             ' Set case insensitivity.
    regEx.Global = True                 ' Set global (match all occurrences)
    tmpStr = Strng
    Set Matches = regEx.Execute(tmpStr) ' Execute search.
    
    For Each Match In Matches           ' Iterate Matches collection.
        Mid(tmpStr, Match.FirstIndex + 1, Len(patrn)) = UCase(Match.SubMatches(0)) & LCase(Match.SubMatches(1)) & UCase(Match.SubMatches(2)) 'UCase(Match.Value)
    Next
    ReplacePattern = tmpStr             ' return result
    Set regEx = Nothing
End Function

Call like this ...
Code:
Dim MyString As String
MyString = "meet bill o'connell at bob mccarthy's"
MyString = StrConv(MyString, vbProperCase) ' Thanks to StephenLeach for StrConv command
MyString = ReplacePattern(MyString, "(\s)(..)(\s)") ' fix " at "
MyString = ReplacePattern(MyString, "(\b.)(')(.\B)")  ' fix "o'c"
MyString = ReplacePattern(MyString, "(\bm)(c)(.\B)")  ' fix "mcc"

Note:
Look up regular expressions in MSDN for more info on \s, \b, \B, . etc

Caveats:
This function assumes that you always match three segments (\b.)(')(.\B) for example, and it uppercases the first and third segments of the match. It does not enumerate the Match object (use Match.SubMatches.Count). Some more time would make this more flexible.



Hope this helps ...
 
Oops, forget, you also need to add a reference in your project to "Microsoft VBScript Regular Expressions 5.5" (I am using VB6 SP5 on Win2K - version may differ on your system??).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top