那可以做一个简单的替换啊for i=65 to 90 strings=replace(strings,chr(i),vbnullstring) strings=replace(strings,chr(i+32),vbnullstring) next
也可以用字符串操作dim i as long dim str1 as string str="abcd123"for i=1 to len(str) if asc(mid(str,i,1))>=48 and asc(mid(str,i,1))<=57 then str1=str1 & mid(str,i,1) end if next msgbox str1
语句有点问题,asc不能正确执行 再看看行吗
如果数字一定在右边: If str Like "*[A-Za-z]*" Then For i = Len(str) To 1 Step -1 tmp = Mid(str, i) If Not IsNumeric(tmp) Then Exit For Next i str = Mid(tmp, 2) End If
不是不能执行,key="a4" 当第二次循时,直接跳过了
Str是关键字,不要用关键字做变量,我没有写清楚。不好意思.Dim i As Long Dim str1 As String Strs = "abcd123"For i = 1 To Len(Strs) If Asc(Mid(Strs, i, 1)) >= 48 And Asc(Mid(Strs, i, 1)) <= 57 Then str1 = str1 & Mid(Strs, i, 1) End If Next MsgBox str1
'使用证则表达式的例子: '引用Microsoft VBScript Regular Expressions libraryDim reg As New RegExp Dim strTest As String Dim regPattern As String Dim Matches As MatchCollection Dim mtch As Match'Dim blnFound As BooleanstrTest = "A1234B4567" regPattern = "\d+$"With reg .Pattern = regPattern If .Test(strTest) Then Set Matches = .Execute(strTest)
For Each mtch In Matches strTest = mtch.Value Next mtch End If End With Set reg = Nothing顺便说一句,对于字母后面的数字,val()只能返回0.
我用的是 dim i as long dim str1 as string str="abcd123"for i=1 to len(str) if IsNumeric(Mid(key, i, 1)) then str1=str1 & mid(str,i,1) end if next msgbox str1
我只要123
但数字长度不定长,不能用right()
有时数字是12
strings=replace(strings,chr(i),vbnullstring)
strings=replace(strings,chr(i+32),vbnullstring)
next
dim str1 as string
str="abcd123"for i=1 to len(str)
if asc(mid(str,i,1))>=48 and asc(mid(str,i,1))<=57 then
str1=str1 & mid(str,i,1)
end if
next
msgbox str1
再看看行吗
If str Like "*[A-Za-z]*" Then
For i = Len(str) To 1 Step -1
tmp = Mid(str, i)
If Not IsNumeric(tmp) Then Exit For
Next i
str = Mid(tmp, 2)
End If
当第二次循时,直接跳过了
Dim str1 As String
Strs = "abcd123"For i = 1 To Len(Strs)
If Asc(Mid(Strs, i, 1)) >= 48 And Asc(Mid(Strs, i, 1)) <= 57 Then
str1 = str1 & Mid(Strs, i, 1)
End If
Next
MsgBox str1
'引用Microsoft VBScript Regular Expressions libraryDim reg As New RegExp
Dim strTest As String
Dim regPattern As String
Dim Matches As MatchCollection
Dim mtch As Match'Dim blnFound As BooleanstrTest = "A1234B4567"
regPattern = "\d+$"With reg
.Pattern = regPattern
If .Test(strTest) Then
Set Matches = .Execute(strTest)
For Each mtch In Matches
strTest = mtch.Value
Next mtch
End If
End With
Set reg = Nothing顺便说一句,对于字母后面的数字,val()只能返回0.
dim i as long
dim str1 as string
str="abcd123"for i=1 to len(str)
if IsNumeric(Mid(key, i, 1)) then
str1=str1 & mid(str,i,1)
end if
next
msgbox str1