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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct case text strings?

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
0
0
US
I have a project where I have to go through and Proper Case names (Private and Company names). There are a few problems I did not realize when using something like StrConv to Proper Case a string. One problem I did not foresee is the apostrophes. Take for Example "WENDY'S RESTAURANT". StrConv seems to see it actually as "WENDY S RESTAURANT" so proper case comes out as "Wendy'S Restaurant" which is incorrect. While I could make my own Proper Case function I was wondering if there was already some way to handle this. C# seems to use String.Format to accomplish this, but if an equivalent exists for vb.net'S String.Format. If it does I couldn'T find one example they all dealt with using it to format numbers and dates. Any suggestions would be greatly appreciated.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I looked up a few things on Google. Seems people are suggesting the old style VB functions. Here's one example: This seems to work


Code:
        Dim s As String = "WENDY'S RESTAURANT"
        MessageBox.Show(Microsoft.VisualBasic.StrConv(s, Microsoft.VisualBasic.VbStrConv.ProperCase))
 
Thanks, but that is the one I was using that doesn't work correctly. The S after the apostrophe is capitalized.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
The code I posted above doesn't work for you? I get a lower case s when I run the code.
 
There seems to be a space between the apostrophe and the "S".
 
@RiverGuy nope. I even change my code to look exactly like yours except I still used MsgBox and I did it to a variable before the msgbox. I did stop it before the msgbox to make sure that it was comming out right and didn't just look right. Or I should say wrong still.

@shavon Nope, no space. It is how certain text blocks out so it seems like there is a space.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I was doing this last week and found it much more complicated than I at first thought. I found several solutions of varying complexity and ended up with the one below which seems to cover most eventualities. There are exceptions like de Beer and De'ath which need a lookup table I think.
Sorry it's in VBA, it ended up in an Access programme, but is pretty easy to convert

Code:
Function ProperCase(AnyText As String) As String

          'Convert passed text to all lowercase. Use ProperCase() as you would a built-in Access function.
          'If passed value is a null, ignore all the stuff below.
         
        If IsNull(Nz(AnyText, Null)) Then GoTo Exit_ProperCase
             
          Dim intCounter As Integer, OneChar As String
         
          'First convert to initial cap, followed by all lowercase.
        AnyText = UCase$(Left$(AnyText, 1)) & LCase$(Mid$(AnyText, 2))
         
          'Look at each character, starting at the second character.
        For intCounter = 2 To Len(AnyText)
            OneChar = Mid$(AnyText, intCounter, 1)
              'If current character (OneChar) is a space or hyphen...
            Select Case OneChar
                  Case "-", "/", ".", "'", "&"
                      '...convert the character after space/hyphen/slash/period/apostrophe/ampersand to uppercase.
                      ' Such as A.B.C. Industries, Sharron O'Conner, B&B Mfg
                    AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
                 
                Case "c"
                      ' Take care of the McAfee's, McDonalds & McLaughlins and such
                    If Mid$(AnyText, intCounter - 1, 1) = "M" Then
                        AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
                   End If
         
               Case " "
                   Select Case Mid$(AnyText, intCounter + 1, 2)
                          Case "de"
                              'Add any other exceptions here Example: Oscar de La Hoya
                           AnyText = Left$(AnyText, intCounter) & LCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
         
                       Case Else
                              ' Example: A B C Manufacturing
                           AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
         
                   End Select
           End Select
       Next
         
          'All done, return current contents of AnyText variable.
       ProperCase = AnyText
         
Exit_ProperCase:
         
End Function
 
Cool. Thanks. I'll take a look at it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen,

As you discovered propercase is handy but limited. It also has problems with names such as O'Malley and addresses with NE, NW etc. As suggested the best way is write your own function to handle case.

If you are using VS.2008 you can easily create a module to extend a the string class so it includes your custom ProperCase function making it easy to use, such as:

tbx.Text = tbx.Text.ProperCase

Here is the function and Module code I use.

Code:
Imports System.Runtime.CompilerServices

Module Mod_ProperCase
    <Extension()> _
        Function ProperCase(ByVal inputString As String)
        Dim ar As String()
        Dim ind As Integer = -1
        Dim outputString As New System.Text.StringBuilder

        If inputString.Equals("") Or inputString Is Nothing Then
            Return ""
        End If
        Try
            inputString = inputString.ToLower

            ar = inputString.Split(" ")
            For x As Integer = 0 To ar.Length - 1

                Mid(ar(x), 1, 1) = Mid(ar(x), 1, 1).ToUpper

                ind = ar(x).IndexOf("'")
                If ind = 1 Then
                    Mid(ar(x), ind + 2, 1) = Mid(ar(x), ind + 2, 1).ToUpper
                End If

                ind = ar(x).LastIndexOf(".")
                If ind > -1 AndAlso ind < ar(x).Length - 1 Then
                    Mid(ar(x), ind + 2, 1) = Mid(ar(x), ind + 2, 1).ToUpper
                End If

                If ar(x).Equals("Ne", StringComparison.CurrentCultureIgnoreCase) Or _
                   ar(x).Equals("Nw", StringComparison.CurrentCultureIgnoreCase) Or _
                   ar(x).Equals("Se", StringComparison.CurrentCultureIgnoreCase) Or _
                   ar(x).Equals("Sw", StringComparison.CurrentCultureIgnoreCase) Then

                    ar(x) = ar(x).ToUpper
                End If
                outputString.Append(ar(x) + " ")
            Next
            Return outputString.ToString.Trim
        Catch ex As Exception
            Return inputString
        End Try
    End Function

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top