Reputation: 273
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.
Upvotes: 0
Views: 946
Reputation: 943
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
Upvotes: 1