|
|
Title | Convert strings between Pascal case, camel case, and proper case in Visual Basic .NET |
Description | This example shows how to convert strings between Pascal case, camel case, and proper case in Visual Basic .NET. |
Keywords | strings, camelCase, PascalCase, Pascal case, camel case, proper case, add spaces, Visual Basic .NET, VB.NET |
Categories | Strings |
|
|
In Pascal case, each word is capitalized as in ThisStringIsPascalCased.
In camel case, each word except the first is capitalized as in thisStringIsCamelCased.
In proper case, each word is capitalized and separated by spaces as in This String Is Proper Cased.
This example uses string extensions to convert strings between these three formats.
The following code shows the ToPascalCase string extension.
|
|
' Convert the string to Pascal case.
<Extension()> _
Public Function ToPascalCase(ByVal the_string As String) As _
String
' If there are 0 or 1 characters, just return the
' string.
If (the_string Is Nothing) Then Return the_string
If (the_string.Length < 2) Then Return _
the_string.ToUpper()
' Split the string into words.
Dim words() As String = the_string.Split( _
New Char() {}, _
StringSplitOptions.RemoveEmptyEntries)
' Combine the words.
Dim result As String = ""
For Each word As String In words
result &= _
word.Substring(0, 1).ToUpper() & _
word.Substring(1)
Next word
Return result
End Function
|
|
The code splits the string into words separated by spaces. It then loops through the words, capitalizing them and adding them together.
The following code shows the ToCamelCase string extension.
|
|
' Convert the string to camel case.
<Extension()> _
Public Function ToCamelCase(ByVal the_string As String) As _
String
' If there are 0 or 1 characters, just return the
' string.
If (the_string Is Nothing OrElse the_string.Length < 2) _
Then Return the_string
' Split the string into words.
Dim words() As String = the_string.Split( _
New Char() {}, _
StringSplitOptions.RemoveEmptyEntries)
' Combine the words.
Dim result As String = words(0).ToLower()
For i As Integer = 1 To words.Length - 1
result &= _
words(i).Substring(0, 1).ToUpper() & _
words(i).Substring(1)
Next i
Return result
End Function
|
|
The ToCamelCase extension is similar to the ToPascalCase extension except it doesn't capitalize the first word.
The following code shows the ToProperCase string extension.
|
|
' 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
' If there are 0 or 1 characters, just return the
' string.
If (the_string Is Nothing) Then Return the_string
If (the_string.Length < 2) Then Return _
the_string.ToUpper()
' Start with the first character.
Dim result As String = the_string.Substring(0, _
1).ToUpper()
' Add the remaining characters.
For i As Integer = 1 To the_string.Length - 1
If (Char.IsUpper(the_string(i))) Then result &= " "
result &= the_string(i)
Next i
Return result
End Function
|
|
ToProperCase loops through the characters in the string and inserts a space in front of each capital letter. Notice how it uses the IsUpper function to determine whether a character is upper case.
Note that this code does not handle multi-character abbreviations (such as USSenator).
|
|
|
|
|
|