'得到字符串中括号里的内容 Public Function GetKh(str As String) As String On Error Resume Next Dim intFirstLocation, intLastLocation As Integer intFirstLocation = InStr(1, str, "(", vbTextCompare) intLastLocation = InStrRev(str, ")") GetKh = Mid(str, intFirstLocation + 1, intLastLocation - intFirstLocation - 1) GetKh = Trim(GetKh)End Function
Dim s1 Dim s2 Dim str As String str = "(0111022)123(0111023)asda(0111024)asdas(0111024)1212)(1212)("
s1 = Split(str, ")", , vbTextCompare) For i = 0 To UBound(s1) - 1 s2 = Split(s1(i), "(", , vbTextCompare) If UBound(s2) > 0 Then Debug.Print s2(UBound(s2)) End If Next i
Dim str As String Dim str1 As String str = "(0111022)"str1 = Split(Split(str, "(")(1), ")")(0) MsgBox str1
result=split(split(str1,"(")(1),")")(0)
正则表达式:引用 Microsoft VBScript Regular Expression x.xDim re1 As RegEx, re2 As RegEx Dim str As String Dim data, num, itemstr = "(0111022)123(0111023)asda(0111024)asdas(0111024)1212)(1212)(" str = Replace(str, "(", "(") str = Replace(str, ")", ")")re1.Pattern = "\(\d+\)" '设置模板为左右括号和数字 re1.Global = True Set data = re1.Execute(str) '提取所有匹配项re2.Pattern = "[\(\)]" '设置模板为左右括号 re2.Global = True For Each item In data num = re2.Replace(item, "") '消除匹配项的括号 Debug.Print num Next
Public Function GetKh(str As String) As String
On Error Resume Next
Dim intFirstLocation, intLastLocation As Integer
intFirstLocation = InStr(1, str, "(", vbTextCompare)
intLastLocation = InStrRev(str, ")")
GetKh = Mid(str, intFirstLocation + 1, intLastLocation - intFirstLocation - 1) GetKh = Trim(GetKh)End Function
str = "(0111022)"
str = Replace(str, "(", "")
str = Replace(str, ")", "")
Debug.Print str
fj182(阿花)的方法好毒啊
楼主可以根据自己的需要自行修改一下。
s$=Mid$(strVar,2,len(strVar)-1)
fj182(阿花)的方法好毒啊多谢夸奖。
Dim str As String
str = "(0111022)(0111023)"
str = Replace(str, "(", vbTab)
str = Replace(str, ")", vbTab)
str = Replace(str, vbTab & vbTab, vbTab)
If Left(str, 1) = vbTab Then str = Mid(str, 2)
If Right(str, 1) = vbTab Then str = Left(str, Len(str) - 1) Debug.Print str其实,这样的问题最好用正则表达式。
Dim s2
Dim str As String
str = "(0111022)123(0111023)asda(0111024)asdas(0111024)1212)(1212)("
s1 = Split(str, ")", , vbTextCompare)
For i = 0 To UBound(s1) - 1
s2 = Split(s1(i), "(", , vbTextCompare)
If UBound(s2) > 0 Then
Debug.Print s2(UBound(s2))
End If
Next i
Dim str1 As String
str = "(0111022)"str1 = Split(Split(str, "(")(1), ")")(0)
MsgBox str1
Dim str As String
Dim data, num, itemstr = "(0111022)123(0111023)asda(0111024)asdas(0111024)1212)(1212)("
str = Replace(str, "(", "(")
str = Replace(str, ")", ")")re1.Pattern = "\(\d+\)" '设置模板为左右括号和数字
re1.Global = True
Set data = re1.Execute(str) '提取所有匹配项re2.Pattern = "[\(\)]" '设置模板为左右括号
re2.Global = True
For Each item In data
num = re2.Replace(item, "") '消除匹配项的括号
Debug.Print num
Next