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
 
 
 
 
 
TitleConvert strings between Pascal case, camel case, and proper case in Visual Basic 6
DescriptionThis example shows how to convert strings between Pascal case, camel case, and proper case in Visual Basic 6.
Keywordsstrings, camelCase, PascalCase, Pascal case, camel case, proper case, add spaces, Visual Basic 6, VB 6
CategoriesStrings
 

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 functions to convert strings between these three formats.

The following code shows the ToPascalCase function.

 
' Convert the string to Pascal case.
Public Function ToPascalCase(ByVal the_string As String) As _
    String
Dim words() As String
Dim result As String
Dim i As Integer

    ' If there are 0 or 1 characters, just return the
    ' string.
    If (Len(the_string) < 2) Then
        ToPascalCase = UCase$(the_string)
        Exit Function
    End If

    ' Split the string into words.
    words = Split(the_string)

    ' Combine the words.
    result = ""
    For i = LBound(words) To UBound(words)
        result = result & _
            UCase$(Mid$(words(i), 1, 1)) & _
            Mid$(words(i), 2)
    Next i

    ToPascalCase = 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 function.

 
' Convert the string to camel case.
Public Function ToCamelCase(ByVal the_string As String) As _
    String
Dim words() As String
Dim result As String
Dim i As Integer

    ' If there are 0 or 1 characters, just return the
    ' string.
    If (Len(the_string) < 2) Then
        ToCamelCase = the_string
        Exit Function
    End If

    ' Split the string into words.
    words = Split(the_string)

    ' Combine the words.
    result = LCase$(words(LBound(words)))
    For i = LBound(words) + 1 To UBound(words)
        result = result & _
            UCase$(Mid$(words(i), 1, 1)) & _
            Mid$(words(i), 2)
    Next i

    ToCamelCase = 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 function.

 
' Capitalize the first character and add a space before
' each capitalized letter (except the first character).
Public Function ToProperCase(ByVal the_string As String) As _
    String
Dim words() As String
Dim result As String
Dim i As Integer
Dim ch As String

    ' If there are 0 or 1 characters, just return the
    ' string.
    If (Len(the_string) < 2) Then
        ToProperCase = UCase$(the_string)
        Exit Function
    End If

    ' Start with the first character.
    result = UCase$(Mid$(the_string, 1, 1))

    ' Add the remaining characters.
    For i = 2 To Len(the_string)
        ch = Mid$(the_string, i, 1)
        If (IsUpper(ch)) Then result = result & " "
        result = result & ch
    Next i

    ToProperCase = result
End Function

' Return True if the string is in all caps.
Private Function IsUpper(ByVal ch As String)
    IsUpper = (UCase$(ch) = ch)
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).

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated