沿着a similar question to split CamelCase items into Title Case in Java,我想对VBScript做同样的事情。从本质上讲,我想最终得到一个像这样工作的辅助函数:
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"
在有人问之前......这不是作业作业。 :)在其他可能的用途中,我想将WMI属性名称拆分为人类可读的格式。
这是一个微薄的尝试,从示例Excel宏中拼凑而成:
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"
...这有些分裂,但显然不是最终目标。
答案 0 :(得分:1)
这可能是一种更简单的方法,但这可以为您提供您正在寻找的结果。
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