替换字符串 vb6 中除数字以外的所有内容 [英] Replace everything except numbers in a string vb6
问题描述
好吧,我做了我的研究,看到了很多关于这个的帖子,但在 VB6 中找不到解决方案那么我如何在 VB6 中做到这一点?
well i did my research and seen a lot of posts about this but couldnt find a solution in VB6 so how can i do this in VB6?
假设我有一个字符串:
从前有一个小孩想知道离家比1000
英里还要远……"
" Once upon a time there was a little kid who wonders about going further than 1000
of miles away from home... "
我只想在与字符串分开的字符串中获取数字1000
",并想替换整个字符串,但数字应该保持不变.
i want to get only numbers "1000
" in this string seperated from string and wanna replace the whole string but numbers should stand still.
推荐答案
最简单的方法是遍历字符串并将数字复制到新的字符串中:
The simplest way is to walk the string and copy numbers to a new one:
Function GetNumbers(Value As String) As String
Dim Index As Long
Dim Final As String
For Index = 1 To Len(Value)
If Mid(Value, Index, 1) Like "[0-9]" Then
Final = Final & Mid(Value, Index, 1)
End If
Next
GetNumbers = Final
End Function
结果:
?GetNumbers("abc12def345")
12345
当有很多数字时,这对于长字符串来说效率低下.
This is inefficient with long strings when there are lots of numbers though.
这篇关于替换字符串 vb6 中除数字以外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!