Results 1 to 12 of 12

Thread: Proper Case Or Title Case

Threaded View

  1. #1

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Proper Case Or Title Case

    In VB6 there was a function called StrConv which we were using to convert a string to Proper Case or Title Case, but there is no direct equivalent in .NET. Yes, we can still use this function in vb.net but it's not a .NET way. I mean, StrConv cannot be used in C# directly. To use it in C# we need to add the reference of Microsoft.VisualBasic.Compatibility.dll. And people will not like to do so just to use a single function of that library.

    Some people write their own function to achieve the functionality. They first convert all the letters of the string to lowercase and then loop through all the words and capitalize the first letter of each word of the string. I am not in favor of this approach. I don't like to write long code just to achieve a small functionality. I always look for shortcuts.

    Though there is not direct equivalent for StrConv in .NET, .NET provides us System.Globalization.TextInfo class to overcome the problem. We can use ToTitleCase function of TextInfo class to convert the string in proper case.

    I have written a small function to do the work.

    EDIT: Please see the modified version of ToProperCase function in post #6 below.

    vb.net Code:
    1. Function ToProperCase( _
    2.     ByVal str As String _
    3. ) As String
    4.  
    5.     Dim myTI As System.Globalization.TextInfo
    6.  
    7.     myTI = New System.Globalization.CultureInfo("en-US", False).TextInfo
    8.     str = str.ToLower
    9.     str = myTI.ToTitleCase(str)
    10.     Return str
    11. End Function

    Usage:

    vb.net Code:
    1. Private Sub Button1_Click( _
    2.     ByVal sender As System.Object, _
    3.     ByVal e As System.EventArgs _
    4. ) Handles Button1.Click
    5.  
    6.     MessageBox.Show(ToProperCase("HELLO WORLD!"))
    7. End Sub

    If you want to use ToProperCase() function like this...

    Code:
    Dim str As String = "HELLO WORLD!"
    str = str.ToProperCase()
    ...then you need to use a concept called Extension Methods. Here it goes:

    vb.net Code:
    1. Module ExtensionMethods
    2.  
    3.     <System.Runtime.CompilerServices.Extension()> _
    4.     Function ToProperCase( _
    5.         ByVal str As String _
    6.     ) As String
    7.  
    8.         Dim myTI As System.Globalization.TextInfo
    9.  
    10.         myTI = New System.Globalization.CultureInfo("en-US", False).TextInfo
    11.         str = str.ToLower
    12.         str = myTI.ToTitleCase(str)
    13.         Return str
    14.     End Function
    15.  
    16. End Module

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width