STRCONV把括号作为第一个字母
问题描述:
STRCONV在下面的回报 (HELLO WORLD)的代码 而不是期望 (你好世界) 因为它把第一个括号为单词“HELLO”的第一个字母。 任何人都可以建议如何克服这个问题吗?STRCONV把括号作为第一个字母
Dim MyString
Dim MyCaseString
MyString = "(HELLO WORLD)"
MyCaseString = strConv(MyString, 3)
MsgBox MyCaseString
答
使用MID()来切掉领先“(” - 可以添加回:
Sub test()
Dim MyString
Dim MyCaseString
MyString = "(HELLO WORLD)"
MyCaseString = "(" & StrConv(Mid(MyString, 2), 3)
MsgBox MyCaseString
End Sub
答
我个人用正则表达式做到这一点(你需要。
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
谢谢共产国际。这看起来确实很有趣。我会尽力从明天开始学习。非常感谢你。 –
答
如约翰·科尔曼,使用Replace
功能可以做同样的事情
- 方法1 - >此将替换左括号
- 方法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
非常感谢John Coleman。完美的作品。我非常感谢你。 –