Convert String to Proper Case / Title Case


Proper case/ Title case = First letter of every word is capitalized.
In VB 6.0, we use StrConv() function to convert to proper case.


1 Dim stringVariable as String
2 stringVariable = "hello world"
3 stringVariable = StrConv(stringVariable,vbProperCase)
4 Debug.Print stringvariable 'Prints Hello World

In VB.Net, we could still use the same function, use the Microsoft.VisualBasic Namespace.

1 Dim stringVariable as String
2 stringVariable = "hello world"
3 stringVariable = StrConv(s,VbStrConv.ProperCase)
4 System.Diagnostics.Debug.Writeln (stringvariable) 'Prints Hello World

If you are using c#, you could use the same technique, only point to note is that you will need to pass the locale ID to the StrConv() function (optional param is not possible)

1 string stringVariable = "hello world";
2 stringVariable = Strings.StrConv(s,VbStrConv.ProperCase,new CultureInfo("en-us").LCID);
3 System.Diagnostics.Debug.Writeln (stringvariable); //Prints Hello World


A little more elegant solution is to use TextInfo class to achieve the same.
C# code is shown below,

1 string stringVariable = "hello world";
2 stringVariable = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Input.ToLower());
3 System.Diagnostics.Debug.Writeln (stringvariable); //Prints Hello World

Happy Coding!
Cheers!

Written by Posted in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s