Private Sub Form_Load() Dim str, strtemp As String Dim i As Integer str = "aaa12" For i = 1 To Len(str) If IsNumeric(Mid(str, i, 1)) Then strtemp = strtemp & Mid(str, i, 1) Next MsgBox strtemp End Sub
不解释了,程序很简单,自己看吧: Option ExplicitPrivate Sub Command1_Click() Dim s As String s = "aaaa1234567" MsgBox getnum(s) End SubPrivate Function getnum(ByVal mStr As String) As Long Dim i As Long i = Len(mStr) Dim s As String, ss As String Dim j As Long If i = 0 Then getnum = 0 Exit Function End If For j = 1 To i s = Right(mStr, j) If Val(s) = 0 Then Exit For End If ss = s Next getnum = Val(ss) End Function
Dim S As String S = "aaaa12" Msgbox Val(StrReverse(Format(Val(StrReverse(S)))))
我才傻呢,本来我是倒过来之后用asc()看是不是=48,如果是,则最后是0,就把结果*10,结果有两个0时又不对了,呵呵,也就是说已经考虑到后面有0结果还是栽在0上了,呵呵 如果判断后面有几个0就太繁了,现在想到新办法了~Function StrToNum(ByVal xStr As String) As Long xStr = "1" & StrReverse$(xStr) '^o^ xStr = StrReverse$(Val(xStr)) StrToNum = CLng(Left$(xStr, Len(xStr) - 1)) End Function
StrReverse(val(StrReverse("aaaa123")))
方法1: Private Sub Command2_Click() str1 = "aaa1" str2 = "yhua10" str3 = "aaggga300" str4 = "abcdefg" MsgBox getnum(str1) MsgBox getnum(str2) MsgBox getnum(str3) MsgBox getnum(str4) End Sub Function getnum(ByVal s As String) As String For i = Len(s) To 1 Step -1 If IsNumeric(Right(s, i)) Then getnum = Right(s, i) Exit Function End If Next getnum = "无数字" End Function ============================方法2(方法1特例): Private Sub Command1_Click() str1 = "aaa1" str2 = "aaa10" str3 = "aaa300" MsgBox Replace(str1, "a", "") MsgBox Replace(str2, "a", "") MsgBox Replace(str3, "a", "") End Sub
算法1:处理类似“asdbsf12233ndsdsb123324nddbd1234”的字符串,输出",12233,123324,1234",至于这种格式有什么用?呵呵!你自己用Split函数试试就知道了。Function NumberGetByString1(ByVal pString As String) Dim tOutText As String
Dim tIndexOver As Long
tIndexOver = Len(pString)
Dim tIndex As Long ' Dim tWord As String ' Dim tAscii As Integer ' Dim tIsNum As Boolean '数字检测 Dim tIsNumOld As Boolean '数字检测旧值 Dim tIsNumChange As Boolean '数字检测改变 Dim tSumStr As String
If tIsNumChange Then tOutText = tOutText & tSumStr tSumStr = "" End If
If tIsNum Then tSumStr = tSumStr & tWord Else tSumStr = "," End If
Next
tOutText = tOutText & tSumStr
NumberGetByString1 = tOutText End Function后面还有另一个算法2,稍后给你。
算法2:从信号角度采取过滤法实现,仅适合“aaaaa12313”类似的格式。注意:本方法针对的数字不能大于2的31次方,否则将溢出出错;同时,本算法不兼容负数。必须保证数字在0到&H7FFFFFFF范围内。本算法优点是特别迅速。Function NumberGetByString2(ByVal pString As String) As Long Dim tOutLong As Long
Dim str, strtemp As String
Dim i As Integer
str = "aaa12"
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then strtemp = strtemp & Mid(str, i, 1)
Next
MsgBox strtemp
End Sub
msgbox right(你的字符串,len(你的字符串)-前面字母的长度)
比如:
你的字符串是"aaa123"
msgbox right("aaa123",len("aaa123")-3)
Option ExplicitPrivate Sub Command1_Click()
Dim s As String
s = "aaaa1234567"
MsgBox getnum(s)
End SubPrivate Function getnum(ByVal mStr As String) As Long
Dim i As Long
i = Len(mStr)
Dim s As String, ss As String
Dim j As Long
If i = 0 Then
getnum = 0
Exit Function
End If
For j = 1 To i
s = Right(mStr, j)
If Val(s) = 0 Then
Exit For
End If
ss = s
Next
getnum = Val(ss)
End Function
S = "aaaa12"
Msgbox Val(StrReverse(Format(Val(StrReverse(S)))))
呵呵,当然考虑到了。见http://community.csdn.net/Expert/topic/3106/3106669.xml?temp=.4202845哈哈,还是要在这里Sorry一下
如果判断后面有几个0就太繁了,现在想到新办法了~Function StrToNum(ByVal xStr As String) As Long
xStr = "1" & StrReverse$(xStr) '^o^
xStr = StrReverse$(Val(xStr))
StrToNum = CLng(Left$(xStr, Len(xStr) - 1))
End Function
Private Sub Command2_Click()
str1 = "aaa1"
str2 = "yhua10"
str3 = "aaggga300"
str4 = "abcdefg"
MsgBox getnum(str1)
MsgBox getnum(str2)
MsgBox getnum(str3)
MsgBox getnum(str4)
End Sub
Function getnum(ByVal s As String) As String
For i = Len(s) To 1 Step -1
If IsNumeric(Right(s, i)) Then
getnum = Right(s, i)
Exit Function
End If
Next
getnum = "无数字"
End Function
============================方法2(方法1特例):
Private Sub Command1_Click()
str1 = "aaa1"
str2 = "aaa10"
str3 = "aaa300"
MsgBox Replace(str1, "a", "")
MsgBox Replace(str2, "a", "")
MsgBox Replace(str3, "a", "")
End Sub
Dim tOutText As String
Dim tIndexOver As Long
tIndexOver = Len(pString)
Dim tIndex As Long '
Dim tWord As String '
Dim tAscii As Integer '
Dim tIsNum As Boolean '数字检测
Dim tIsNumOld As Boolean '数字检测旧值
Dim tIsNumChange As Boolean '数字检测改变
Dim tSumStr As String
For tIndex = 1 To tIndexOver
tWord = Mid(pString, tIndex, 1)
tAscii = Asc(tWord)
tIsNumOld = tIsNum
tIsNum = tAscii >= 48 And tAscii <= 57
tIsNumChange = tIsNum Xor tIsNumOld
If tIsNumChange Then
tOutText = tOutText & tSumStr
tSumStr = ""
End If
If tIsNum Then
tSumStr = tSumStr & tWord
Else
tSumStr = ","
End If
Next
tOutText = tOutText & tSumStr
NumberGetByString1 = tOutText
End Function后面还有另一个算法2,稍后给你。
Dim tOutLong As Long
If Not pString = "" Then
Dim tBytes() As Byte
Dim tBytes_Length As Byte
tBytes() = pString
tBytes_Length = UBound(tBytes())
Dim tIndex As Long
Dim tByteValue As Long
Dim tByteIsNum As Boolean
For tIndex = 0 To tBytes_Length Step 2
tByteIsNum = tBytes(tIndex) >= 48 And tBytes(tIndex) <= 57
tByteValue = tByteIsNum And (tBytes(tIndex) - 48)
tOutLong = (tOutLong * 10) + tByteValue
Next
End If
NumberGetByString2 = tOutLong
End Function
Function NumberGetByString3(ByVal pString As String) As String
Dim tOutStr As String
Dim tAscii As Integer
For tAscii = 32 To 47
pString = Replace(pString, Chr(tAscii), "")
Next
For tAscii = 58 To 127
pString = Replace(pString, Chr(tAscii), "")
Next
NumberGetByString3 = pString
End Function