Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a second method to convert strings between Pascal case, camel case, and proper case in Visual Basic .NET
DescriptionThis example shows how to use a second method to convert strings between Pascal case, camel case, and proper case in Visual Basic .NET.
KeywordsPascal case, camel case, proper case, Pascal-case, camel-case, proper-case, capitalize, capitalization, Visual Basic .NET, VB.NET
CategoriesStrings
 
Thanks to Gary Winey for this version.

The example Convert strings between Pascal case, camel case, and proper case in Visual Basic .NET explains one method for converting between Pascal case, camel case, and proper case. This example shows another approach.

The ToPascalCase extension method uses the following code to capitalize the first letter in each of a string's words.

 
' Convert the string to Pascal case.
<Extension()> _
Public Function ToPascalCase(ByVal the_string As String) As _
    String
    Dim info As TextInfo = _
        Thread.CurrentThread.CurrentCulture.TextInfo
    the_string = info.ToTitleCase(the_string)
    Dim parts() As String = the_string.Split(New Char() {}, _
        _
        StringSplitOptions.RemoveEmptyEntries)
    Dim result As String = String.Join(String.Empty, parts)
    Return result
End Function
 
This code uses a TextInfo object's ToTitleCase method to convert the string into title case, which capitalizes the first letter in each of its words. It then splits the string into words and concatenates them together using Join.

The ToCamelCase method uses the following code to capitalize the first letter in each word except for the first word.

 
' Convert the string to camel case.
<Extension()> _
Public Function ToCamelCase(ByVal the_string As String) As _
    String
    the_string = the_string.ToPascalCase()
    Return the_string.Substring(0, 1).ToLower() + _
        the_string.Substring(1)
End Function
 
This code simply calls ToPascalCase and then converts the first letter to lower case.

The following code shows how the ToProperCase method splits a Pascal case or camel case string into words and capitalizes each.

 
' Capitalize the first character and add a space before
' each capitalized letter (except the first character).
<Extension()> _
Public Function ToProperCase(ByVal the_string As String) As _
    String
    Const pattern As String = "(?<=\w)(?=[A-Z])"
    'Const pattern As String = "(?<=[^A-Z])(?=[A-Z])"
    Dim result As String = Regex.Replace(the_string, _
        pattern, " ", RegexOptions.None)
    Return result.Substring(0, 1).ToUpper() + _
        result.Substring(1)
End Function
 
This code uses a regular expression to find places where a word is followed by a capital letter and replaces those locations with a space character to separate the words in the string. The code then capitalizes the first letter in the string and returns the result.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated