判断一个数据中是否包含有空格,
如果数据中包含有空格,则删除从空格起到末尾所有的字符(最好也删除数据中所有非数字型字符)
否则,删除数据中所有非数字型字符比如:kk123  205-kk   应用此函数应成为    kk123 (最好成为:123)
   kk123205kk      应用此函数应成为   123205

解决方案 »

  1.   

    Option ExplicitPrivate Sub Command1_Click()
     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)進行分析下就出來了。
    自己寫阿,當做練習
      

  2.   

    这样:
    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
      

  3.   

    Function getit(ByVal x As String) As String
    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
      

  4.   

    Private Sub Command1_Click()
    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
      

  5.   

    northwolves(狼行天下) 的碰到,如果数字中间夹了字母的话,后面的数字就读不出来了。kk123k1233  205-kk 就不行了 ,应该是1231233,但是getit是123
     fishmans(金脚指) ( ) 的由于用了val,也是问题更严重。
    如果
    getvalue("kk123k1233  205-kk")=0val是碰到第1个不是数字的字母k就停止读取了。
      

  6.   

    //northwolves(狼行天下) 的碰到,如果数字中间夹了字母的话,后面的数字就读不出来了。
    kk123k1233  205-kk 就不行了 ,应该是1231233,但是getit是123
    你的看法有问题northwolves的思路是不管字串中是否有空格,都先调用split函数得到第一个空格前的字串,然后逐一判断这个字串的各个字符是否为数字,是则保留,不是就替换成"",所以应该不会有什么问题
      

  7.   

    哈哈,太easy了。Private Sub Form_Load()
        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
      

  8.   

    哎呀,不好意思,偶的代码与northwolves(狼行天下) 的差不多。发贴的时候没仔细看    @_@             sorry
      

  9.   

    不好意思,我搞错了,没细看northwolves的程序
      

  10.   

    Private Sub Form_Load()
        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
      

  11.   

    仔细一看:Chice_wxg(学)(习) 的程序也有问题,呵呵:)
      

  12.   

    注释错了,不好意思
    Dim lngLength    '长度
      

  13.   

    都是高手
    马上结帖,能介绍一下学VB的方法吗?现在突然发现VB还挺有用的,我学过C语言,当然只是简单的
      

  14.   

    -_-!朗行天下的只是用那个do循环把字符串左边的字母去掉了而已,
    只比fishmans(金脚指) 好了一点,都是用val。
    用val就有问题。
      

  15.   

    看来对val这个函数都搞不清楚啊。 gzhiceberg(天晓得) 的如果碰到 数字中间夹了个字母就有问题啊。一样的毛病。123k123 123 应该是 123123 但是用你的得出123val不能把字符串中所有数字都读出来,读到第1个不是数字的地方就停止读了。
    Val函数 
    返回包含于字符串内的数字,字符串中是一个适当类型的数值。语法Val(string)必要的 string 参数可以是任何有效的字符串表达式.说明Val 函数,在它不能识别为数字的第一个字符上,停止读入字符串。那些被认为是数值的一部分的符号和字符,例如美圆号与逗号,都不能被识别。但是函数可以识别进位制符号 &O(八进制)和 &H(十六进制)。空白、制表符和换行符都从参数中被去掉。下面的返回值为 1615198:Val("    1615 198th Street N.E.")在下面的代码中,Val 为所示的十六进制数值返回十进制数值 -1。Val("&HFFFF")注意 Val 函数只会将句点(.)当成一个可用的小数点分隔符。当使用不同的小数点分隔符时,如在国际版应用程序中,代之以 CDbl 来把字符串转换为数字。
      

  16.   

    看来只能遍历了:Function getit(ByVal x As String) As String
    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
      

  17.   


    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
      

  18.   

    嘿嘿!不用争了!看我的这个函数吧……
    (如果想比速度,记得要编译后再比)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
      

  19.   

    上面给你的函数是按照你的需求写的。不过,我猜想下面这个函数或许会更适合你。下面的函数可以将:Text1932 de1222 22ckmk11转换为 1932 1222 22 11,不同区域出现的数字之间有空格间隔,更容易区分数字之间的区别。Function NumberGetByString2(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 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
      

  20.   

    Function getit(ByVal x As String) As String
    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呀,??????
      

  21.   

    现在给两种方法做一个比较:NumberGetByString_BytesFilter函数:是以我的字节过滤法编写的。NumberGetByString_Replace函数:是我根据楼上几位的代码以替换法编写的。以上函数结果完全一样:在速度方面:测试方式是对内容为"dh12j4j3h1"的字符串进行多次过滤。测试用电脑特地找了一部十分破烂的CR433。不编译状态下结果如下:NumberGetByString_BytesFilter 21978次/秒NumberGetByString_Replace 6765次/秒编译后结果如下:NumberGetByString_BytesFilter 45249次/秒NumberGetByString_Replace 8058次/秒代码如下:Function NumberGetByString_Replace(ByVal pString As String) As String
      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
      

  22.   

    献上最新改进版本:
    编译前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
      

  23.   

    TO KiteGirl(小仙妹) 呵呵,我有兴趣来比较速度~~~~~~~~~Private Sub Form_Load()
        '测试结果
        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
      

  24.   

    哇塞,你们强!!
    谢谢。
    谢谢
    daisy8675(莫依)  fishmans(金脚指) northwolves(狼行天下)  gdami(糖米) ( ) 
     Chice_wxg(学)(习)gzhiceberg(天晓得)  qybao(阿宝) wzboywwf(wzboywwf) KiteGirl(小仙妹)VB版人真好啊,这么多热心肠的人,跑了这么多版块,也没让我有这种感觉,感觉像是回到了学校,热情的兄弟,较真的班长老大,美丽的同桌,嘿嘿,分不是最重要的,最要的是这种气氛,我喜欢:)
    恍惚间,发现思念的影子
    我猛得抬头
    黑暗中----
    你的深情
    让我忘记恐惧
    我爱你们。
      

  25.   

    唉……
    你们怎么全不先看看楼主的要求啊?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
      

  26.   

    第一次写的函数只能去26个英文字母,
    下面这个函数完全可以达到你的要求。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
      

  27.   

    哇!比速度!哪我就再爆个Power的!(看家本领——内存换速度)Private priBytes_NCT() As BooleanPrivate Sub Bytes_SetNCT() '在程序OnLoad时候先运行这个过程初始化表。
      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天!提问场所彻底变赛车场了?呵呵!