asp 字母和数字的字符串,如何提取字符串中的数字。比如:字符串“km16”如何提取字符串数字“16”
asp字母和数字的字符串,如何提取字符串中的数字。比如:字符串“km16”如何提取字符串数字“16”...
asp 字母和数字的字符串,如何提取字符串中的数字。比如:字符串“km16”如何提取字符串数字“16”
展开
2个回答
展开全部
送你一个函数吧,取得部分或全部字符串中的数值部分。当sAll为真时取得字符串中所有数字
' 使用:VarNum = Val(NumberString, [False|True])
Function Val(NumStr, sAll)
If IsNull(NumStr) Or NumStr = "" Then
Val = 0: Exit Function
End If
Dim i, ii, B
Const N = "-.0123456789"
If VarType(sAll) <> 11 And VarType(sAll) <> 2 Then
sAll = False
Else
sAll = CBool(sAll)
End If
For i = 1 To Len(NumStr)
ii = Mid(NumStr, i, 1)
If sAll = True Then '取出所有数字
If InStr(1, N, ii) > 0 Then
Val = Val & ii
B = IsNumeric(Val)
If i > 1 Then
If B = False Or Right(Val, 1) = "-" Then
Val = Left(Val, Len(Val) - 1)
End If
End If
End If
Else '遇到非数字时退出
If InStr(1, N, ii) = 0 Then
Exit For
Else
Val = Val & ii
B = IsNumeric(Val)
If i > 1 Then
If B = False Or Right(Val, 1) = "-" Then
Val = Left(Val, Len(Val) - 1)
Exit For
End If
End If
End If
End If
Next
If IsNumeric(Val) Then
Val = CDbl(Val)
Else
Val = 0
End If
End Function
' 使用:VarNum = Val(NumberString, [False|True])
Function Val(NumStr, sAll)
If IsNull(NumStr) Or NumStr = "" Then
Val = 0: Exit Function
End If
Dim i, ii, B
Const N = "-.0123456789"
If VarType(sAll) <> 11 And VarType(sAll) <> 2 Then
sAll = False
Else
sAll = CBool(sAll)
End If
For i = 1 To Len(NumStr)
ii = Mid(NumStr, i, 1)
If sAll = True Then '取出所有数字
If InStr(1, N, ii) > 0 Then
Val = Val & ii
B = IsNumeric(Val)
If i > 1 Then
If B = False Or Right(Val, 1) = "-" Then
Val = Left(Val, Len(Val) - 1)
End If
End If
End If
Else '遇到非数字时退出
If InStr(1, N, ii) = 0 Then
Exit For
Else
Val = Val & ii
B = IsNumeric(Val)
If i > 1 Then
If B = False Or Right(Val, 1) = "-" Then
Val = Left(Val, Len(Val) - 1)
Exit For
End If
End If
End If
End If
Next
If IsNumeric(Val) Then
Val = CDbl(Val)
Else
Val = 0
End If
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询