VB中求字符串中的空格数

来源:百度知道 编辑:UC知道 时间:2024/05/22 13:29:37
比如我在TEXT控件中输入“1 23 425213 532 23 ”(并不固定)我想求里面的空格数5,怎么求?有什么方法可以做到吗?说的可以稍微清楚点吗?刚学VB不久新手求助呵呵~

dim i as integer
dim j as integer
dim a as string

'* 察看text中每个字符
for i = 1 to len(text1.text)
'* 把第i个字符赋值给a
a = mid(text1.text, i, 1)
'* 如果a是空格,记录下来
if a = " " then
j = j + 1
end if
next i

'* 输出空格数
printf str(j)

Private Function GetSpaceCount(str As String) As Integer
Dim count As Integer
Dim i As Integer
For i = 1 To Len(str)
If Asc(Mid(str, i, 1)) = 32 Then
count = count + 1
End If
Next i
GetSpaceCount = count

End Function
我给你封装的函数,这个函数就是返回一个字符串里的空格数量

明天写给你,现在要熄灯了。

看来不用了,支持楼上的,简单可行。

哈哈,有了,想到一个更简单的。
dim Oldstring as string
dim StringNoSpaces as string

'去掉原串中的所有空格
StringNoSpaces = replace(Oldstring," ","")

NumOfSpaces = Len(OldString)-Len(StringNoSpaces)

这种方法其实是偷懒了,借助了Replace函数。