判断一个数据中是否包含有空格,
如果数据中包含有空格,则删除从空格起到末尾所有的字符(最好也删除数据中所有非数字型字符)
否则,删除数据中所有非数字型字符比如:kk123 205-kk 应用此函数应成为 kk123 (最好成为:123)
kk123205kk 应用此函数应成为 123205
如果数据中包含有空格,则删除从空格起到末尾所有的字符(最好也删除数据中所有非数字型字符)
否则,删除数据中所有非数字型字符比如:kk123 205-kk 应用此函数应成为 kk123 (最好成为:123)
kk123205kk 应用此函数应成为 123205
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim strText As String
i = InStr(1, Text1.Text, " ")
n = Len(Text1.Text)
If i <> 0 And i <> n Then
Text2.Text = Left(Text1.Text, i - 1)
Else
' kk123205kk 应用此函数应成为 123205
'---輪流對asc進行分析 End If
End Sub沒時間寫,這個比較簡單,你用asc(mid(text1.text,n,1)進行分析下就出來了。
自己寫阿,當做練習
private function GetValue(str as string) as string
dim i as integer
if instr(1,str," ") >0 then
str=left(str,instr(1,str," ")-1)
endif
getvalue=cstr(val(str))
end function
getit = Split(x, " ")(0)
Do While Not IsNumeric(Left(getit, 1))
getit = Replace(getit, Left(getit, 1), "")
Loop
getit = Val(getit)
End FunctionPrivate Sub Command1_Click()
MsgBox getit("kk123 205-kk")
MsgBox getit("kk123205kk")
End Sub
MsgBox MyVal("kk123 205-kk")
MsgBox MyVal("kk123205kk")
End Sub
Function MyVal(str1 As String) As String
Dim i As Integer, j As Integer
For i = 1 To Len(str1)
j = Asc(Mid(str1, i, 1))
If j = vbKeySpace Then Exit For
If j < 58 And j > 47 Then MyVal = MyVal & Chr(j)
Next i
End Function
fishmans(金脚指) ( ) 的由于用了val,也是问题更严重。
如果
getvalue("kk123k1233 205-kk")=0val是碰到第1个不是数字的字母k就停止读取了。
kk123k1233 205-kk 就不行了 ,应该是1231233,但是getit是123
你的看法有问题northwolves的思路是不管字串中是否有空格,都先调用split函数得到第一个空格前的字串,然后逐一判断这个字串的各个字符是否为数字,是则保留,不是就替换成"",所以应该不会有什么问题
MsgBox GetVal("kk123205kk")
MsgBox GetVal("kk123 205-kk")
End SubFunction GetVal(ByVal s As String) As Long
Dim i As Long
For i = 1 To Len(s) '先把非数字替换为空格
If Not IsNumeric(Mid(s, i, 1)) Then Mid(s, i, 1) = " "
Next
GetVal = Split(Trim(s), " ")(0) '小技巧而已,可以省一个变量
End Function
MsgBox GetNum("kk123 205-kk")
MsgBox GetNum("kk123205kk")
End SubPublic Function GetNum&(ByVal strData)
Dim lngStart& '第一个数
Dim lngLength '第二个数
Dim blnStart As Boolean
Dim lngCnt&
For lngCnt = 1 To Len(strData)
If IsNumeric(Mid(strData, lngCnt, 1)) Then
If Not blnStart Then
lngStart = lngCnt
blnStart = True
lngLength = 1
Else
lngLength = lngLength + 1
End If
Else
If blnStart Then
Exit For
End If
End If
Next
If lngStart <> 0 Then
GetNum = Val(Mid(strData, lngStart, lngLength))
End If
End Function
Dim lngLength '长度
马上结帖,能介绍一下学VB的方法吗?现在突然发现VB还挺有用的,我学过C语言,当然只是简单的
只比fishmans(金脚指) 好了一点,都是用val。
用val就有问题。
Val函数
返回包含于字符串内的数字,字符串中是一个适当类型的数值。语法Val(string)必要的 string 参数可以是任何有效的字符串表达式.说明Val 函数,在它不能识别为数字的第一个字符上,停止读入字符串。那些被认为是数值的一部分的符号和字符,例如美圆号与逗号,都不能被识别。但是函数可以识别进位制符号 &O(八进制)和 &H(十六进制)。空白、制表符和换行符都从参数中被去掉。下面的返回值为 1615198:Val(" 1615 198th Street N.E.")在下面的代码中,Val 为所示的十六进制数值返回十进制数值 -1。Val("&HFFFF")注意 Val 函数只会将句点(.)当成一个可用的小数点分隔符。当使用不同的小数点分隔符时,如在国际版应用程序中,代之以 CDbl 来把字符串转换为数字。
Dim i As Long, temp() As String
getit = Split(x, " ")(0)
ReDim temp(1 To Len(getit))
For i = 1 To Len(getit)
temp(i) = Mid(getit, i, 1)
If Not IsNumeric(temp(i)) Then temp(i) = ""
Next
getit = Join(temp, "")
Erase temp
End FunctionPrivate Sub Command1_Click()
MsgBox getit("kk123 205-kk")
MsgBox getit("kk123s205kk")
End Sub
Function GetVal(ByVal s As String) As String
Dim i As Long
GetVal = ""
For i = 1 To Len(s)
If Mid(s, i, 1) = " " Then '遇到空格就退出循环
Exit For
End If
If IsNumeric(Mid(s, i, 1)) Then '是数字就保留
GetVal = GetVal & Mid(s, i, 1)
End if
Next
End Function
(如果想比速度,记得要编译后再比)Private Sub Command1_Click()
Text1.Text = NumberGetByString(Text1.Text)
End SubFunction NumberGetByString(ByVal pString As String) As String
Dim tBytes() As Byte
Dim tBytes_Length As Long
tBytes() = pString
tBytes_Length = UBound(tBytes())
Dim tIndex As Long
Dim tOutBytes() As Byte
Dim tOutBytes_Index As Long
Dim tByteIsNumber As Boolean
Dim tByteOver As Boolean
tOutBytes_Index = 0
For tIndex = 0 To tBytes_Length Step 2
tByteOver = tBytes(tIndex) = 32
If tByteOver Then Exit For
tByteIsNumber = (tBytes(tIndex) > 47) And (tBytes(tIndex) < 58)
If tByteIsNumber Then
ReDim Preserve tOutBytes(tOutBytes_Index + 1)
tOutBytes(tOutBytes_Index) = tBytes(tIndex)
tOutBytes_Index = tOutBytes_Index + 2
End If
Next
NumberGetByString = tOutBytes()
End Function
Dim tBytes() As Byte
Dim tBytes_Length As Long
tBytes() = pString
tBytes_Length = UBound(tBytes())
Dim tIndex As Long
Dim tOutBytes() As Byte
Dim tOutBytes_Index As Long
Dim tByteIsNumber As Boolean
Dim tByte As Byte
tOutBytes_Index = 0
For tIndex = 0 To tBytes_Length Step 2
tByteIsNumber = (tBytes(tIndex) > 47) And (tBytes(tIndex) < 58)
tByte = (tByteIsNumber And tBytes(tIndex)) + ((Not tByteIsNumber) And 32)
ReDim Preserve tOutBytes(tOutBytes_Index + 1)
tOutBytes(tOutBytes_Index) = tByte
tOutBytes_Index = tOutBytes_Index + 2
Next
Dim tOutString As String
tOutString = tOutBytes()
Do While CBool(InStr(tOutString, " "))
tOutString = Replace(tOutString, " ", " ")
Loop
NumberGetByString2 = tOutString
End Function
getit = Split(x, " ")(0)
Do While Not IsNumeric(Left(getit, 1))
getit = Replace(getit, Left(getit, 1), "")
Loop
getit = Val(getit)
End FunctionPrivate Sub Command1_Click()
MsgBox getit("kk123 205-kk")
MsgBox getit("kk123205kk")
End Sub
to northwolves(狼行天下)
假如msgbox getit("1kk123")
是不是只输出1呀,??????
Dim tOutString As String
Dim tString_Length As Long
Dim tIndex As Long
Dim tString As String
Dim tWord As String
tString = Split(pString, " ")(0)
tString_Length = Len(tString)
tOutString = tString
For tIndex = 1 To tString_Length
tWord = Mid(tString, tIndex, 1)
If Not IsNumeric(tWord) Then
tOutString = Replace(tOutString, tWord, "")
End If
Next
NumberGetByString_Replace = tOutString
End FunctionFunction NumberGetByString_BytesFilter(ByVal pString As String) As String
Dim tBytes() As Byte
Dim tBytes_Length As Long
tBytes() = pString
tBytes_Length = UBound(tBytes())
Dim tIndex As Long
Dim tOutBytes() As Byte
Dim tOutBytes_Index As Long
Dim tByteIsNumber As Boolean
Dim tByteOver As Boolean
tOutBytes_Index = 0
For tIndex = 0 To tBytes_Length Step 2
tByteOver = tBytes(tIndex) = 32
If tByteOver Then Exit For
tByteIsNumber = (tBytes(tIndex) > 47) And (tBytes(tIndex) < 58)
If tByteIsNumber Then
ReDim Preserve tOutBytes(tOutBytes_Index + 1)
tOutBytes(tOutBytes_Index) = tBytes(tIndex)
tOutBytes_Index = tOutBytes_Index + 2
End If
Next
NumberGetByString_BytesFilter = tOutBytes()
End Function
编译前30395次/秒 编译后73529次/秒Function NumberGetByString_BytesFilterPro(ByVal pString As String) As String
Dim tBytes() As Byte
Dim tBytes_Length As Long
tBytes() = pString
tBytes_Length = UBound(tBytes())
If tBytes_Length < 0 Then Exit Function
Dim tIndex As Long
Dim tOutBytes() As Byte
Dim tOutBytes_Index As Long
Dim tByteIsNumber As Boolean
Dim tByteOver As Boolean
ReDim tOutBytes(tBytes_Length)
tOutBytes_Index = 0
For tIndex = 0 To tBytes_Length Step 2
tByteOver = tBytes(tIndex) = 32
If tByteOver Then Exit For
tByteIsNumber = (tBytes(tIndex) > 47) And (tBytes(tIndex) < 58)
If tByteIsNumber Then
tOutBytes(tOutBytes_Index) = tBytes(tIndex)
tOutBytes_Index = tOutBytes_Index + 2
End If
Next
ReDim Preserve tOutBytes(tOutBytes_Index)
NumberGetByString_BytesFilterPro = tOutBytes()
End Function
'测试结果
Debug.Print NumberGetByString_BytesFilterPro("dh12j4j3h1 a123")
Debug.Print GetVal("dh12j4j3h1 a123")
Dim i As Long, st As Double
For i = 1 To 100
NumberGetByString_BytesFilterPro ("dh12j4j3h1 a123")
GetVal ("dh12j4j3h1 a123")
Next
st = Timer
For i = 1 To 10000
NumberGetByString_BytesFilterPro ("dh12j4j3h1 a123")
Next
MsgBox (Timer - st) * 1000
'============================
For i = 1 To 100
NumberGetByString_BytesFilterPro ("dh12j4j3h1 a123")
GetVal ("dh12j4j3h1 a123")
Next
st = Timer
For i = 1 To 10000
GetVal ("dh12j4j3h1 a123")
Next
MsgBox (Timer - st) * 1000
End Sub'这是我的代码~~~~~~~~~~ 稍微快一点。just for fun !
Function GetVal(ByVal s As String) As String
Dim i As Long, j As Long
Dim b() As Byte
Dim bb() As Byte
b = s
ReDim bb(Len(s))
For i = 0 To UBound(b) Step 2
If b(i) = &H20 Then Exit For
If &H30 <= b(i) And b(i) <= &H39 Then
bb(j) = b(i)
j = j + 2
End If
Next
GetVal = Left(bb, j \ 2) '这样快 10% -20 %
'GetVal = bb '这样快 20% -30 % ,但末尾有空字符
End Function
'你的代码,原封未动
Function NumberGetByString_BytesFilterPro(ByVal pString As String) As String
Dim tBytes() As Byte
Dim tBytes_Length As Long
tBytes() = pString
tBytes_Length = UBound(tBytes())
If tBytes_Length < 0 Then Exit Function
Dim tIndex As Long
Dim tOutBytes() As Byte
Dim tOutBytes_Index As Long
Dim tByteIsNumber As Boolean
Dim tByteOver As Boolean
ReDim tOutBytes(tBytes_Length)
tOutBytes_Index = 0
For tIndex = 0 To tBytes_Length Step 2
tByteOver = tBytes(tIndex) = 32
If tByteOver Then Exit For
tByteIsNumber = (tBytes(tIndex) > 47) And (tBytes(tIndex) < 58)
If tByteIsNumber Then
tOutBytes(tOutBytes_Index) = tBytes(tIndex)
tOutBytes_Index = tOutBytes_Index + 2
End If
Next
ReDim Preserve tOutBytes(tOutBytes_Index)
NumberGetByString_BytesFilterPro = tOutBytes()
End Function
谢谢。
谢谢
daisy8675(莫依) fishmans(金脚指) northwolves(狼行天下) gdami(糖米) ( )
Chice_wxg(学)(习)gzhiceberg(天晓得) qybao(阿宝) wzboywwf(wzboywwf) KiteGirl(小仙妹)VB版人真好啊,这么多热心肠的人,跑了这么多版块,也没让我有这种感觉,感觉像是回到了学校,热情的兄弟,较真的班长老大,美丽的同桌,嘿嘿,分不是最重要的,最要的是这种气氛,我喜欢:)
恍惚间,发现思念的影子
我猛得抬头
黑暗中----
你的深情
让我忘记恐惧
我爱你们。
你们怎么全不先看看楼主的要求啊?Private Function GetNumber(ByVal SourceString As String) As String
On Error GoTo GetNumberERR:
Dim i As Integer
Dim S() As String
SourceString = SourceString & " "
S = Split(SourceString, " ", -1, 1)
SourceString = S(0)
SourceString = UCase(SourceString)
For i = 65 To 90
SourceString = Replace(SourceString, Chr(i), "")
Next
GetNumber = SourceString
Exit Function
GetNumberERR:
MsgBox Err.Description, vbCritical, "Error"
GetNumber = ""
End Function
下面这个函数完全可以达到你的要求。Private Function GetNumber(ByVal SourceString As String) As String
On Error GoTo GetNumberERR:
Dim i As Integer
Dim S() As String
Dim sN As String
SourceString = SourceString & " "
S = Split(SourceString, " ", -1, 1)
SourceString = S(0)
For i = 1 To Len(SourceString)
sN = Mid(SourceString, i, 1)
If IsNumeric(sN) Then
GetNumber = GetNumber & sN
End If
Next
Exit Function
GetNumberERR:
MsgBox Err.Description, vbCritical, "Error"
GetNumber = ""
End Function
Dim tIndex As Long
ReDim priBytes_NCT(255)
For tIndex = 48 To 57
priBytes_NCT(tIndex) = True
Next
End SubFunction NumberGetByString_UltraBytesFilter(ByVal pString As String) As String
Dim tBytes() As Byte
Dim tBytes_Length As Long
tBytes() = pString
tBytes_Length = UBound(tBytes())
If tBytes_Length < 0 Then Exit Function
Dim tIndex As Long
Dim tOutBytes() As Byte
Dim tOutBytes_Index As Long
Dim tByteIsNumber As Boolean
Dim tByteOver As Boolean
ReDim tOutBytes(tBytes_Length)
tOutBytes_Index = 0
For tIndex = 0 To tBytes_Length Step 2
tByteOver = tBytes(tIndex) = 32
If tByteOver Then Exit For
tByteIsNumber = priBytes_NCT(tBytes(tIndex)) '连判断都不做了,彻底造反!
If tByteIsNumber Then
tOutBytes(tOutBytes_Index) = tBytes(tIndex)
tOutBytes_Index = tOutBytes_Index + 2
End If
Next
ReDim Preserve tOutBytes(tOutBytes_Index)
NumberGetByString_UltraBytesFilter = tOutBytes() '输出部分使用兄台的办法,会更快。
End Function天!提问场所彻底变赛车场了?呵呵!