如何计算vb.net中的空格和字符数?

时间:2014-08-30 10:47:44

标签: vb.net

我想计算在文本框字段中输入的空格和字符数。 实施例

 textbox1.text - " 1 2 3 4 5 6 7 a b c"

我想计算空格数和字符数

然后结果必须是..

将空格变为整数,将char变为整数

spaces = 10,char = 10

3 个答案:

答案 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)