Results 1 to 12 of 12

Thread: Proper Case Or Title Case

  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

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Proper Case Or Title Case

    Thanks for the code....
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Proper Case Or Title Case

    I've posted similar code before in the VB.NET forum but, interestingly, I can't find it with a search at the moment. I wanted to point out a couple of small issues I have with your implementation.

    I went with something more succinct:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. <Extension()> _
    4. Public Module StringExtensions
    5.  
    6.     Public Function ToTitleCase(ByVal source As String) As String
    7.         Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source)
    8.     End Function
    9.  
    10. End Module
    There's no point calling ToLower first when you're going to call ToTitleCase immediately after. Also, I would think that using the current culture would be better than creating a CultureInfo explicitly, although I can't say that I've tried that on a system that isn't set to English. Finally, it's a small thing but I do prefer the name ToTitleCase to match the method in the TextInfo class, given that ToUpper and ToLower both match.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

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

    Re: Proper Case Or Title Case

    Quote Originally Posted by jmcilhinney
    There's no point calling ToLower first when you're going to call ToTitleCase immediately after.
    ToTitleCase function of TextInfo class doesn't converts the string to Title Case if we pass CAPITAL WORDS. That is why I am converting it first to lower case and then to Title Case. Next, I used the name ToProperCase because I think it's more popular/familiar than TitleCase. I agree that we can use Current Culture rather than creating a new one.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Proper Case Or Title Case

    Quote Originally Posted by Deepak Sakpal
    ToTitleCase function of TextInfo class doesn't converts the string to Title Case if we pass CAPITAL WORDS.
    Well, that I didn't know. That's good information. With that in mind, and the fact that I missed an Extension attribute on the method before, I'd change my code to:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. <Extension()> _
    4. Public Module StringExtensions
    5.  
    6.     <Extension()> _
    7.     Public Function ToTitleCase(ByVal source As String) As String
    8.         Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source.ToLower())
    9.     End Function
    10.  
    11. End Module
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

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

    Modified version

    Here is the new modified version of ToProperCase function based on the suggestions.

    vb.net Code:
    1. Function ToProperCase( _
    2.     ByVal source As String _
    3. ) As String
    4.  
    5.     source = source.ToLower
    6.     source = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source)
    7.     Return source
    8. End Function

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Proper Case Or Title Case

    My only comment would be on the use of the ToLower function... and, yes, I realize the reason you used it was because of some words being in all caps... but there are some cases where that is the proper use and should be left alone... I'm not saying don't use it... but just be aware that sometimes that's the desired result. That's all.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

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

    Re: Proper Case Or Title Case

    Quote Originally Posted by techgnome
    My only comment would be on the use of the ToLower function... and, yes, I realize the reason you used it was because of some words being in all caps... but there are some cases where that is the proper use and should be left alone... I'm not saying don't use it... but just be aware that sometimes that's the desired result. That's all.

    -tg
    Yes, that's true. Also there are some words like McDonald should also be handled. What if I add an optional parameter to the ToProperCase function which will accept the list of words that should not be converted to proper case. What say?

  9. #9
    New Member
    Join Date
    Jun 2009
    Posts
    8

    Re: Proper Case Or Title Case

    I need to propercase names in a database.

    the apostrphes are killing me.

    I used the program and it does not handle names with apostrophes
    to handle this i just split the name on the apostrophe and passes the second half of the name back through the function then reassembled the name

    I did the same for names that start with "Mc"
    So far i have not found a name this does not handle

    Code:
    For Each employee As employee In employeelist
                If employee.last_name.Contains("'") Then
                    Dim splitname As String()
                    splitname = employee.last_name.Split("'")
                    employee.last_name = splitname(0) & "'" & ToProperCase(splitname(1))
                End If
                If employee.last_name.StartsWith("Mc") Then
                    Dim lasthalf As String = employee.last_name.Substring(2, employee.last_name.Length - 2)
                    employee.last_name = "Mc" & ToProperCase(lasthalf)
                End If
            Next
    Last edited by mbarnett; Jun 15th, 2009 at 04:22 PM.

  10. #10
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Proper Case Or Title Case

    I also have a simple function which convert to Proper Case :

    Code:
       TextBox4.Text = StrConv(TextBox4.Text, vbProperCase)
    Last edited by manhit45; Jun 15th, 2009 at 07:12 PM.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  11. #11

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

    Re: Proper Case Or Title Case

    Quote Originally Posted by manhit45 View Post
    I also have a simple function which convert to Proper Case :

    Code:
       TextBox4.Text = StrConv(TextBox4.Text, vbProperCase)
    Yes you can use StrConv and I already mentioned that in the very first post. It's a vb6 function. And, I don't see any problem with my ToProperCase function if you pass a string containing apostrophes.

    Code:
    MessageBox.Show(ToProperCase("i'm loving it."))

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Proper Case Or Title Case

    Quote Originally Posted by Deepak Sakpal View Post
    I don't see any problem with my ToProperCase function if you pass a string containing apostrophes.
    I'm guessing that that was a reference to names like "O'Connell", which would come out as "O'connell". There just is no simple way to handle stuff like that because it's got nothing to do with titles specifically. It's just a special case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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