计算Vb6中的字符而不包括空格(字符串)
答案 0 :(得分:5)
只需使用Len()
和Replace()
来检索字符串的长度并删除空格。例如:
Const strText As String = "The quick brown fox"
Debug.Print "Original length: " & Len(strText) ' => 23
Debug.Print "Length w/o spaces: " & Len(Replace$(strText, " ", "")) ' => 16
答案 1 :(得分:0)
Function NonSpaceCount(ByRef Text As String) As Long
Dim I As Long
Dim S As Long
S = Len(Text)
Do While S
S = InStr(I + 1, Text, " ")
If S Then
NonSpaceCount = NonSpaceCount + S - (I + 1)
I = S
Else
NonSpaceCount = NonSpaceCount + Len(Text) - I
End If
Loop
End Function
速度并不总是一切,但这应该比大多数替代品更快。