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:
Function ToProperCase( _
ByVal str As String _
) As String
Dim myTI As System.Globalization.TextInfo
myTI = New System.Globalization.CultureInfo("en-US", False).TextInfo
str = str.ToLower
str = myTI.ToTitleCase(str)
Return str
End Function
Usage:
vb.net Code:
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
MessageBox.Show(ToProperCase("HELLO WORLD!"))
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:
Module ExtensionMethods
<System.Runtime.CompilerServices.Extension()> _
Function ToProperCase( _
ByVal str As String _
) As String
Dim myTI As System.Globalization.TextInfo
myTI = New System.Globalization.CultureInfo("en-US", False).TextInfo
str = str.ToLower
str = myTI.ToTitleCase(str)
Return str
End Function
End Module
Re: Proper Case Or Title Case
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:
Imports System.Runtime.CompilerServices
<Extension()> _
Public Module StringExtensions
Public Function ToTitleCase(ByVal source As String) As String
Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source)
End Function
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.
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.
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:
Imports System.Runtime.CompilerServices
<Extension()> _
Public Module StringExtensions
<Extension()> _
Public Function ToTitleCase(ByVal source As String) As String
Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source.ToLower())
End Function
End Module
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
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?
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
Re: Proper Case Or Title Case
I also have a simple function which convert to Proper Case :
Code:
TextBox4.Text = StrConv(TextBox4.Text, vbProperCase)
Re: Proper Case Or Title Case
Quote:
Originally Posted by
manhit45
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."))
Re: Proper Case Or Title Case
Quote:
Originally Posted by
Deepak Sakpal
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.