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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25581521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:08:38  来源:igfitidea点击:

How to count the number of spaces and characters in vb.net?

vb.net

提问by user3418036

I want to count the number of spaces and characters inputted in a textbox field. Example

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

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

I want to count the number of spaces and the characters

我想计算空格和字符的数量

Then the result must be..

那么结果一定是..

Dim spaces as integer, char as integer

Dim 空格为整数,char 为整数

spaces = 10 , char = 10

空格 = 10 ,字符 = 10

回答by

 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 Eachwill iterate each character within the stringthen it will check whether cis a space or not. if it is a space then increment the spaceCountelse increment the lettrtCount

For Each将迭代每个字符,string然后它会检查是否c是一个空格。如果它是一个空格然后增加spaceCountelse 增加lettrtCount

回答by Govinda Rajbhar

You can do as you want in a sort way.

您可以以某种方式为所欲为。

Try it

尝试一下

Dim count As Integer = 0
textbox1.text = " 1 2 3 4 5 6 7 a b c"
count = textbox1.text.Split(" ").Length -1

回答by user9166871

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)