VB.NET How to Properly Capitalize Someone's Name

by Tim at 03:57PM

Often the ToLower() and ToUpper() and other methods like that are very handy but if you want to capitalize someone's name properly there are a couple of gotchas that come up.  Here's how I handle the McDonalds and O'Reillys of the world.  This is written in VB.NET but the syntax could be changed for almost any programming language. 

    Public Function FormatName(ByVal value As String) As String

        Select Case True

            Case value.Length > 3 AndAlso value.ToUpper.StartsWith("M") AndAlso value.ToUpper.Substring(1, 1) = "C"
                ' Example: McDonald
                Return value.Substring(0, 1).ToUpper & value.Substring(1, 1).ToLower & _
                       value.Substring(2, 1).ToUpper & value.Substring(3).ToLower

            Case value.Length > 3 AndAlso value.ToUpper.StartsWith("O") AndAlso value.Substring(1, 1) = "'"
                ' Example: O'Reilly
                Return value.Substring(0, 1).ToUpper & value.Substring(1, 1) & _
                       value.Substring(2, 1).ToUpper & value.Substring(3).ToLower

            Case Else
                Return StrConv(value, VbStrConv.ProperCase)

        End Select

    End Function

Comments

Add comment


 

biuquote
  • Comment
  • Preview
Loading