ARTICLE

How to Format a String in Proper Case or Title Case in VB.NET

Posted by James Mouchett Articles | Visual Basic 2010 August 15, 2009
This is a basic function used to format a string into it proper case equivalent without the use of the VB6 (visualbasic.dll) namespace.
 
Reader Level:

Introduction:

This article describes a simple approach to covert a string to the proper case or title case as was available in previous versions of Visual Basic.  This function is only a few lines of code but will return any string with the first letter of each word in upper case while all other letters are in lower case.  The function shown may be of use if you have an application that receives data in upper or lower case format and needs to be shown in title case or proper case.

The Code:

T
he code is very simple and does not require much of an explanation.  Basically, all that is done, is the receiving string is split into multiple strings using the space character as the delimiter.  Then a loop iterates through each word and capitalizes the first letter of each word.  Then the rest of the letters in each iterated string is converted to lower case.  After all the words have been iterated through the return string is built and then finally returned in proper or title case.  The code is located below:

Public Shared Function ToProper(ByVal Str As String) As String

' Author:      James Mouchett
' Build Date:  March 16, 2009
' Use:         To format a string to proper case / title case without the
'              need of the visualbasic.dll (VB6) namespace.

' Storage String For Output
Dim OutStr As String = String.Empty

' Used For Holding Each Word Separated By A Space
Dim Words() As String = Split(Str, " ")

' Loop Through All The Words In The String
For A = 0 To Words.GetUpperBound(0)

    
' Retrieve The Word In The Words Array For Processing
    
Dim TempWord As String = Words(A)

    
' Loop Through All The Characters In The String
    
For B = 0 To TempWord.Length - 1

         
If B = 0 Then
               ' Make The First Character Uppercase
              
OutStr += Char.ToUpper(TempWord(B))
         
Else
              
' Make The Other Characters Lowercase
              
OutStr += Char.ToLower(TempWord(B))
         
End If

         
' Add Spaces If Any Are Necessary
         
If A <> Words.GetUpperBound(0) And B = TempWord.Length - 1 Then
               OutStr += " "
         
End If

     Next
Next

' Return Formatted String
Return OutStr

End Function

Summary:

In whole, this is almost like the .ToProper or StrConv(ToProper) function from the visualbasic.dll or VB6 namespace.  However, this function was written with the intention of removing the dependency of the VB6 namespace and to further utilize the .NET OOP programming style instead.

Login to add your contents and source code to this article
share this article :
post comment
 

Dim propercase as string = StrConv(SomeText, VbStrConv.ProperCase)

Posted by Jim Stevens Mar 28, 2011

To simplify

Public
Function ToProper(ByVal str As String) As String

Dim newString As New StringBuilder

For Each s As String In str.Split(" ")

newString.Append(s.Substring(0, 1).ToUpper & s.Substring(1).ToLower + " ")

Next

Return newString.ToString.Trim

End Function

Posted by Adam Downs Sep 01, 2010

You are correct, and that is my fault for overlooking this.  I have gone back through the above code and have modified it accordingly.  The corrected code snippet is below:

Public Shared Function ToProper2(ByVal Str As String) As String
        Dim builder As StringBuilder = New StringBuilder

        ' Used For Holding Each Word Separated By A Space
        Dim Words() As String = Split(Str, " ")

        ' Loop Through All The Words In The String
        For A = 0 To Words.GetUpperBound(0)

            ' Loop Through All The Characters In The String
            For B = 0 To Words(A).Length - 1

                If B = 0 AndAlso Words(A)(B) <> " " Then
                    ' Make The First Character Uppercase
                    builder.Append(Char.ToUpper(Words(A)(B)))
                Else
                    ' Make The Other Characters Lowercase
                    builder.Append(Char.ToLower(Words(A)(B)))
                End If

                ' Add Spaces If Any Are Necessary
                If A <> Words.GetUpperBound(0) And B = Words(A).Length - 1 Then
                    builder.Append(" ")
                End If

            Next
        Next

        Return builder.ToString
    End Function

This code snippet above not only helps with saving memory space, but is also slightly faster.  Thank you for your input and help.

Posted by James Mouchett Aug 17, 2009

I wonder why you didn't use the StringBuilder. As you know, in .NET strings are immutable and when you write something like
Dim a As String = "hello"
a += " world"
you created two distinct strings in your heap memory. One containing "hello" and another one containing "hello world" (plus the \0 char, of course).

So, you're going to create N+1 distinct strings where N is the number of the characters in the starting one. :)

Posted by Renato Aug 17, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor