我想计算在文本框字段中输入的空格和字符数。 实施例
textbox1.text - " 1 2 3 4 5 6 7 a b c"
我想计算空格数和字符数
然后结果必须是..
将空格变为整数,将char变为整数
spaces = 10,char = 10
答案 0 :(得分:1)
Dim spaceCount, lettercount As Integer
spaceCount= 0
lettercount = 0
Dim s As String = " 1 2 3 4 5 6 7 a b c"
For Each c As Char In s
If c = " " Then
spaceCount+= 1
Else
lettercount += 1
End If
Next
MsgBox(charcount)
MsgBox(lettercount)
For Each
将迭代string
中的每个字符,然后它会检查c
是否为空格。如果是空格,则增加spaceCount
其他增加lettrtCount
答案 1 :(得分:0)
您可以按照自己的意愿排序。
试一试
Dim count As Integer = 0
textbox1.text = " 1 2 3 4 5 6 7 a b c"
count = textbox1.text.Split(" ").Length -1
答案 2 :(得分:0)
Dim longstring As String = "aab c d e"
Dim TotalLength As Integer = longstring.Length
Dim TotalSpaces() As String = Split(longstring, " "
Dim TotalChars As Integer = TotalLength - (TotalSpaces.Length - 1)