Converting Text to Proper Case

Posted Wed, Jul 22 2009 10:19 by Deborah Kurata

When displaying text to the user as a title or message, you may want to ensure that the text is presented in proper case (also called title case).

If you are not familiar with the term “title case”, it basically means upper casing the first letter of each word as you would do in a proper title. For example: “Last Name” or “Customer Status Code”.

NOTE: Be sure to define a reference to System.Globalization.

In C#:

string sampleString = "this is a title";
CultureInfo currentCulture =
         System.Threading.Thread.CurrentThread.CurrentCulture;
TextInfo currentInfo = currentCulture.TextInfo;
sampleString = currentInfo.ToTitleCase(sampleString);

In VB:

Dim sampleString As String = "this is a title"
Dim currentCulture As CultureInfo = _
         System.Threading.Thread.CurrentThread.CurrentCulture
Dim currentInfo As TextInfo = currentCulture.TextInfo
sampleString = currentInfo.ToTitleCase(sampleString)

This code uses the CultureInfo to ensure that the casing is set appropriately based on the current culture. It then uses the ToTitleCase method of the TextInfo class to convert to the appropriate casing.

In VB there is another option to accomplish this task:

Dim sampleString As String = "this is a title"
sampleString = StrConv(sampleString, vbProperCase)

This code uses the StrConv function that is part of the Microsoft.VisualBasic namespace. This function could also be used from C# if you define a reference to the Microsoft.VisualBasic namespace.

The result is:

This Is A Title

Notice that this does not follow the proper case rules that define articles and conjunctions as lower case. Rather, it converts the first letter of every word to upper case and the remainder of the text to lower case.

Enjoy!

Filed under: , , ,

Comments

# re: Converting Text to Proper Case

Saturday, August 15, 2009 10:18 AM by John Burns

I'm working with data collected over eons (e.g. 20-30 yrs... mainframe, to DOS DB, to Win DB...). Names have been converted to UpperCase and I would like to make them "normal" - however, I don't want to end up like many bulk mailing printouts - which fail to put proper casing to names like O' Neil, or Mc Murrey, or de la Roma --- and the end up looking like: O'neil, or Mcmurrey, or De la roma

I suspect this would take a huge name database to match against or to just have a huge typefest where names are corrected via "data entry".

Know any automated way?

# re: Converting Text to Proper Case

Monday, August 17, 2009 10:10 AM by Deborah Kurata

Hi John -

Thank you for visiting my blog.

One option for handling more complex names is to use the techniques described in this post and build in a set of rules to correct the most common occurrences, such as O'Neil and McMurrey. You could then have the code print to a log for any cases it questioned (like having multiple words in the last name). Then people would only need to look at these exceptions and not every single last name in the database.

Hope this helps.

# re: Converting Text to Proper Case

Wednesday, August 19, 2009 4:09 PM by Omar

thanks for your efforts

Public Class DotNetUtility
   Public Shared Function ConvertingTextCase(ByVal str As String, ByVal stringcase As TextCase) As String
       If str Is Nothing Then
           Return String.Empty
       End If

       Dim ci As System.Globalization.TextInfo = Application.CurrentCulture.TextInfo

       Select Case stringcase

           Case TextCase.LowerCase
               str = ci.ToLower(str)
               Exit Select

           Case TextCase.None
               str = str
               Exit Select

           Case TextCase.TitleCase
               str = ci.ToTitleCase(str)
               Exit Select

           Case TextCase.UpperCase
               str = ci.ToUpper(str)
               Exit Select

       End Select
       Return str
   End Function

   Public Shared Function IsRightToLeft(ByVal str As String) As Boolean
       If str Is Nothing Then
           Exit Function
       End If

       Dim ci As System.Globalization.TextInfo = Application.CurrentCulture.TextInfo
       Dim rtl As Boolean

       If ci.IsRightToLeft = True Then
           rtl = True
       Else
           rtl = False
       End If

       Return rtl
   End Function

   Public Enum TextCase
       None = 0
       LowerCase = 1
       TitleCase = 2
       UpperCase = 3

   End Enum

End Class

 

# re: Converting Text to Proper Case

Wednesday, August 19, 2009 4:30 PM by Deborah Kurata

Hi Omar -

Thank you for sharing your code.

NOTE: I removed some of the excess line spacing that was inserted by the editor here.

Thanks again!

# re: Converting Text to Proper Case

Monday, August 31, 2009 9:02 AM by Kevin S Gallagher

Focusing on making text proper case, if the incoming text is all uppercase then .ToLower method is needed.

using the following

 <Runtime.CompilerServices.Extension()> _
   Public Function ProperCase(ByVal value As String) As String
       Dim TI As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
       Return TI.ToTitleCase(value.ToLower)
   End Function

with this data

       Dim Mary = (From name In New String() _
                   {"MARY JONE'S", "mary jone's", "Mary jone'S"} _
                   Select name.ProperCase).ToList
       Mary.ForEach(AddressOf Console.WriteLine)

Outputs

MARY JONE's
Mary Jone's
Mary Jone's
MARY JONE's
Mary Jone's
Mary Jone's

Now add .ToLower

   <Runtime.CompilerServices.Extension()> _
   Public Function ProperCase(ByVal value As String) As String
       Dim TI As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
       Return TI.ToTitleCase(value.ToLower)
   End Function

We get proper casing correct

Mary Jone's
Mary Jone's
Mary Jone's
Mary Jone's
Mary Jone's
Mary Jone's

 

# re: Converting Text to Proper Case

Monday, October 19, 2009 9:02 PM by raveee

Good! But the code could not get me to convert my Heading to either lower or upper.  Any suggestions?

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: