0

Along the lines of a similar question to split CamelCase items into Title Case in Java, I want to do the same with VBScript. Essentially, I want to end up with a helper function that works like this:

    SplitCamelCase ( "lowercase" )       ' Returns: "lowercase"
    SplitCamelCase ( "Class" )           ' Returns: "Class"
    SplitCamelCase ( "MyClass" )         ' Returns: "My Class"
    SplitCamelCase ( "HTML" )            ' Returns: "HTML"
    SplitCamelCase ( "PDFLoader" )       ' Returns: "PDF Loader"
    SplitCamelCase ( "AString" )         ' Returns: "A String"
    SplitCamelCase ( "SimpleXMLParser" ) ' Returns: "Simple XML Parser"
    SplitCamelCase ( "GL11Version" )     ' Returns: "GL 11 Version"

And before anyone asks... This is not for a homework assignment. :) Among other possible uses, I want to split WMI Property Names into a human-readable format.

Here's a meager attempt, cobble together from an example Excel Macro:

Function SplitCamel( txt )
  Dim Hold , i
  Hold = Left(txt, 1)
  For i = 2 To Len(txt) Step 1
    If Asc(Mid(txt, i, 1)) > 96 Then
      Hold = Hold & Mid(txt, i, 1)
    Else
      Hold = Hold & " " & Mid(txt, i, 1)
    End If
  Next
  SplitCamel = Hold
End Function

WScript.Echo SplitCamel( "CSDVersion" ) ' Returns: "C S D Version"

... which does some splitting, but obviously is not what the end goal is.

Community
  • 1
  • 1
treehead
  • 264
  • 1
  • 11
  • So what is the problem? You don´t know how to fix the CSD example, and are looking for an algorithm, or do you suspect the code shown doesn´t work? – TheBlastOne Mar 26 '14 at 07:04
  • Well, "C S D Version" is not the output I'm looking for. As with "PDFLoader" and "SimpleXMLParser"examples, I want multi-capital acronyms embedded in the string to stay intact. So the correct return value would be "CSD Version" in that case. – treehead Mar 26 '14 at 14:29

1 Answers1

1

Probably an easier way to do this, but this should give you the results you're looking for.

MsgBox SplitCamelCase ("lowercase")         ' Returns: "lowercase"
MsgBox SplitCamelCase ("Class")             ' Returns: "Class"
MsgBox SplitCamelCase ("My Class")          ' Returns: "My Class"
MsgBox SplitCamelCase ("HTML")              ' Returns: "HTML"
MsgBox SplitCamelCase ("PDF Loader")        ' Returns: "PDF Loader"
MsgBox SplitCamelCase ("A String")          ' Returns: "A String"
MsgBox SplitCamelCase ("Simple XML Parser") ' Returns: "Simple XML Parser"
MsgBox SplitCamelCase ("GL 11 Version")     ' Returns: "GL 11 Version"
MsgBox SplitCamelCase ("CSDVersionCamel")   ' Returns: "CSD Version Camel"

Function SplitCamelCase(strTxt)
    Dim strNew, i

    strNew = ""

    For i = 1 To Len(strTxt)

        If Mid(strTxt, i, 1) = " " Then
            strNew = strNew & Mid(strTxt, i, 1)
        ElseIf IsNumeric(Mid(strTxt, i, 1)) Then
            If i > 1 Then
                If IsNumeric(Mid(strTxt, i - 1, 1)) Then
                    strNew = strNew & Mid(strTxt, i, 1)
                ElseIf Mid(strTxt, i - 1, 1) = " " Then
                    strNew = strNew & Mid(strTxt, i, 1)
                Else
                    strNew = strNew & " " & Mid(strTxt, i, 1)
                End If
            Else
                strNew = strNew & " " & Mid(strTxt, i, 1)
            End If
        ElseIf Mid(strTxt, i, 1) = UCase(Mid(strTxt, i, 1)) Then
            If i > 1 Then               
                If Mid(strTxt, i - 1, 1) = UCase(Mid(strTxt, i - 1, 1)) Then
                    If Mid(strTxt, i + 1, 1) = " " Then 
                        strNew = strNew & Mid(strTxt, i, 1)
                    ElseIf Mid(strTxt, i + 1, 1) = "" Then 
                        strNew = strNew & Mid(strTxt, i, 1) 
                    ElseIf IsNumeric(Mid(strTxt, i + 1, 1)) = True Then 
                        strNew = strNew & Mid(strTxt, i, 1)
                    ElseIf Mid(strTxt, i + 1, 1) = LCase(Mid(strTxt, i + 1, 1)) Then
                        If Mid(strTxt, i - 1, 1) = " " Then
                            strNew = strNew & Mid(strTxt, i, 1)
                        Else
                            strNew = strNew & " " & Mid(strTxt, i, 1)
                        End If
                    Else
                        strNew = strNew & Mid(strTxt, i, 1)
                    End If
                ElseIf Mid(strTxt, i - 1, 1) <> " " Then            
                    strNew = strNew & " " & Mid(strTxt, i, 1)
                Else
                    strNew = strNew & Mid(strTxt, i, 1)
                End If
            Else
                strNew = strNew & Mid(strTxt, i, 1)
            End If
        Else
            strNew = strNew & Mid(strTxt, i, 1)
        End If  
    Next

    SplitCamelCase = Trim(strNew)

End Function
JustSomeQuickGuy
  • 933
  • 1
  • 10
  • 21
  • Thanks! This is very close to the output I got after playing with the function a bit more. Both of our versions have an extra space between some words, though, so I need to go back and figure out how to account for that. – treehead Mar 26 '14 at 16:11
  • Sorry, I changed something last second that did that double spacing. Please try again, updated the code. – JustSomeQuickGuy Mar 26 '14 at 16:24
  • That did it! The output matches! – treehead Mar 26 '14 at 17:14