2014-03-26 28 views
0

沿着a similar question to split CamelCase items into Title Case in Java的路線,我想用VBScript做同樣的事情。從本質上講,我想用一個輔助函數工作原理是這樣結束了:如何將CamelCase轉換爲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

那麼是什麼問題?您不知道如何解決CSD示例,並且正在尋找算法,或者您懷疑顯示的代碼不工作? – TheBlastOne

+0

那麼,「C S D版本」不是我正在尋找的輸出。與「PDFLoader」和「SimpleXMLParser」示例一樣,我希望字符串中嵌入的多首字母縮略詞保持不變。所以在這種情況下,正確的返回值將是「CSD版本」。 – treehead

回答

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 
+0

謝謝!這非常接近我在使用該函數後得到的輸出。不過,我們的兩個版本之間有一些額外的空間,所以我需要回去弄清楚如何解釋。 – treehead

+0

對不起,我最後一秒做了一些改變,做了兩倍的間距。請再試一次,更新代碼。 – QuickNull

+0

這樣做!輸出匹配! – treehead

相關問題