2015-06-28 1290 views
1

STRCONV在下面的回報 (HELLO WORLD)的代碼 而不是期望 (你好世界) 因爲它把第一個括號爲單詞「HELLO」的第一個字母。 任何人都可以建議如何克服這個問題嗎?STRCONV把括號作爲第一個字母

Dim MyString 
    Dim MyCaseString 
     MyString = "(HELLO WORLD)" 
     MyCaseString = strConv(MyString, 3) 
    MsgBox MyCaseString 

回答

1

使用MID()來切掉領先「(」 - 可以添加回:

Sub test() 
    Dim MyString 
    Dim MyCaseString 
    MyString = "(HELLO WORLD)" 
    MyCaseString = "(" & StrConv(Mid(MyString, 2), 3) 
    MsgBox MyCaseString 
End Sub 
+0

非常感謝John Coleman。完美的作品。我非常感謝你。 –

1

我個人用正則表達式做到這一點(你需要。

Dim regex As New RegExp 
Dim matches As MatchCollection 
Dim word As Match 

Dim sample As String 
sample = "(HELLO WORLD)" 

With regex 
    .Pattern = "\w+" 
    .Global = True 
    Set matches = .Execute(sample) 
End With 

For Each word In matches 
    sample = Replace$(sample, word, StrConv(word, vbProperCase)) 
Next word 

Debug.Print sample 
+0

謝謝共產國際。這看起來確實很有趣。我會盡力從明天開始學習。非常感謝你。 –

0

如約翰·科爾曼,使用Replace功能可以做同樣的事情

:微軟的VBScript正則表達式5.5)的引用
  1. 方法1 - >此將替換左括號
  2. 方法2 - >此將取代左/右括號

方法1:

Sub Test1() 

    Dim MyString 
    Dim MyCaseString 

    MyString = "(HELLO WORLD)" 
    MyCaseString = "(" & StrConv(Replace(MyString, "(", ""), 3) 

    MsgBox MyCaseString 
End Sub 

方法2:

Sub Test2() 

    Dim MyString 
    Dim MyCaseString 

    MyString = "(HELLO WORLD)" 

    mystr = Replace(Replace(MyString, "(", ""), ")", "") 
    MyCaseString = "(" & StrConv(mystr, vbProperCase) & ")" 

    MsgBox MyCaseString 
End Sub 
相關問題