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.
KeywordsPascal case, camel case, proper case, Pascal-case, camel-case, proper-case, capitalize, capitalization, 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 into PascalCase.
Public Function ToPascalCase(ByVal the_string As String) As _
    String
Dim words() 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)

    ' Capitalize each word.
    For i = LBound(words) To UBound(words)
        If (Len(words(i)) > 0) Then
            Mid$(words(i), 1, 1) = UCase$(Mid$(words(i), 1, _
                1))
        End If
    Next i

    ' Combine the words.
    ToPascalCase = Join(words, "")
End Function
 
This code splits the string into words, capitalizes them, and then concatenates them.

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

 
' Convert the string into camelCase.
Public Function ToCamelCase(ByVal the_string As String) As _
    String
Dim result As String

    result = ToPascalCase(the_string)
    If Len(result) > 0 Then
        Mid$(result, 1, 1) = LCase$(Mid$(result, 1, 1))
    End If

    ToCamelCase = result
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).
Public Function ToProperCase(ByVal the_string As String) 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 (UCase$(ch) = ch) Then result = result & " "
        result = result & ch
    Next i

    ToProperCase = result
End Function
 
This code loops through the string, inserting a space character in front of each capitalized letter except the first.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated