.Net, ASP.Net, C#, VB.net, SQL Server, Xml, CSS, Design Patterns related tips, tricks, code snippets, articles, links, thoughts, etc. from Binu & Subi Thayamkery.

Binu Thayamkery is a seasoned software architect with more than 13 years of experience in developing enterprise grade connected systems using Microsoft Technologies. In his current position as a lead consultant-solution architect with Prudential Financial, he is working on architecture of next generation investment reporting framework using .net 3.5/WCF/AJAX, etc. He holds a Masters Degree in Computer Science from Colorado State University. Subi Thayamkery is an experienced software developer with more than 8 years of developing various application software systems ranging from workflow automation systems to compliance management tools. She currently works as a technology consultant for Prudential Financial where she helps develop a new system for corportate governance department. She holds an Electrical Engineering degree from New Jersey Institute of Technology.

Wednesday, April 12, 2006

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!