这个函数怎么写??

fuqing1 2004-07-12 08:19:15
判断一个数据中是否包含有空格,
如果数据中包含有空格,则删除从空格起到末尾所有的字符(最好也删除数据中所有非数字型字符)
否则,删除数据中所有非数字型字符

比如:kk123 205-kk 应用此函数应成为 kk123 (最好成为:123)
   kk123205kk 应用此函数应成为 123205

...全文
326 31 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuqing1 2004-07-14
  • 打赏
  • 举报
回复
没人比了吗?结帖了
这分都不好给,切。
谢谢大家拉。
KiteGirl 2004-07-13
  • 打赏
  • 举报
回复
哇!比速度!哪我就再爆个Power的!(看家本领——内存换速度)

Private priBytes_NCT() As Boolean

Private Sub Bytes_SetNCT() '在程序OnLoad时候先运行这个过程初始化表。
Dim tIndex As Long
ReDim priBytes_NCT(255)
For tIndex = 48 To 57
priBytes_NCT(tIndex) = True
Next
End Sub

Function 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

天!提问场所彻底变赛车场了?呵呵!
vansoft 2004-07-13
  • 打赏
  • 举报
回复
第一次写的函数只能去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
vansoft 2004-07-13
  • 打赏
  • 举报
回复
唉……
你们怎么全不先看看楼主的要求啊?

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
fuqing11 2004-07-13
  • 打赏
  • 举报
回复
哇塞,你们强!!
谢谢。
谢谢
daisy8675(莫依) fishmans(金脚指) northwolves(狼行天下) gdami(糖米) ( )
Chice_wxg(学)(习)gzhiceberg(天晓得) qybao(阿宝) wzboywwf(wzboywwf) KiteGirl(小仙妹)

VB版人真好啊,这么多热心肠的人,跑了这么多版块,也没让我有这种感觉,感觉像是回到了学校,热情的兄弟,较真的班长老大,美丽的同桌,嘿嘿,分不是最重要的,最要的是这种气氛,我喜欢:)


恍惚间,发现思念的影子
我猛得抬头
黑暗中----
你的深情
让我忘记恐惧


我爱你们。
Chice_wxg 2004-07-13
  • 打赏
  • 举报
回复
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
viena 2004-07-13
  • 打赏
  • 举报
回复
弓虽!
KiteGirl 2004-07-13
  • 打赏
  • 举报
回复
献上最新改进版本:
编译前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

KiteGirl 2004-07-13
  • 打赏
  • 举报
回复
现在给两种方法做一个比较:

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 Function

Function 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
wzboywwf 2004-07-12
  • 打赏
  • 举报
回复
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 Function

Private Sub Command1_Click()
MsgBox getit("kk123 205-kk")
MsgBox getit("kk123205kk")
End Sub


to northwolves(狼行天下)
假如msgbox getit("1kk123")
是不是只输出1呀,??????
gdami 2004-07-12
  • 打赏
  • 举报
回复
别人的要求不是这样的。
KiteGirl 2004-07-12
  • 打赏
  • 举报
回复
上面给你的函数是按照你的需求写的。不过,我猜想下面这个函数或许会更适合你。

下面的函数可以将: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

KiteGirl 2004-07-12
  • 打赏
  • 举报
回复
嘿嘿!不用争了!看我的这个函数吧……
(如果想比速度,记得要编译后再比)

Private Sub Command1_Click()
Text1.Text = NumberGetByString(Text1.Text)
End Sub

Function 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
qybao 2004-07-12
  • 打赏
  • 举报
回复

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
northwolves 2004-07-12
  • 打赏
  • 举报
回复
看来只能遍历了:

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 Function

Private Sub Command1_Click()
MsgBox getit("kk123 205-kk")
MsgBox getit("kk123s205kk")
End Sub
gdami 2004-07-12
  • 打赏
  • 举报
回复
看来对val这个函数都搞不清楚啊。

gzhiceberg(天晓得) 的如果碰到 数字中间夹了个字母就有问题啊。一样的毛病。

123k123 123 应该是 123123 但是用你的得出123

val不能把字符串中所有数字都读出来,读到第1个不是数字的地方就停止读了。




Val函数
返回包含于字符串内的数字,字符串中是一个适当类型的数值。

语法

Val(string)

必要的 string 参数可以是任何有效的字符串表达式.

说明

Val 函数,在它不能识别为数字的第一个字符上,停止读入字符串。那些被认为是数值的一部分的符号和字符,例如美圆号与逗号,都不能被识别。但是函数可以识别进位制符号 &O(八进制)和 &H(十六进制)。空白、制表符和换行符都从参数中被去掉。

下面的返回值为 1615198:

Val(" 1615 198th Street N.E.")

在下面的代码中,Val 为所示的十六进制数值返回十进制数值 -1。

Val("&HFFFF")

注意 Val 函数只会将句点(.)当成一个可用的小数点分隔符。当使用不同的小数点分隔符时,如在国际版应用程序中,代之以 CDbl 来把字符串转换为数字。
gdami 2004-07-12
  • 打赏
  • 举报
回复
-_-!

朗行天下的只是用那个do循环把字符串左边的字母去掉了而已,
只比fishmans(金脚指) 好了一点,都是用val。
用val就有问题。
fuqing1 2004-07-12
  • 打赏
  • 举报
回复
都是高手
马上结帖,能介绍一下学VB的方法吗?现在突然发现VB还挺有用的,我学过C语言,当然只是简单的
gzhiceberg 2004-07-12
  • 打赏
  • 举报
回复
注释错了,不好意思
Dim lngLength '长度
rainstormmaster 2004-07-12
  • 打赏
  • 举报
回复
仔细一看:Chice_wxg(学)(习) 的程序也有问题,呵呵:)
加载更多回复(11)

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧