2014-08-30 32 views
-1

我想計算在文本框字段中輸入的空格和字符數。 實施例如何計算vb.net中的空格和字符數?

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

我要計數的空格數和字符

然後其結果必然是..

昏暗空格作爲整數,焦炭用作整數

空間= 10 ,char = 10

+2

可能重複http://stackoverflow.com/questions/5193893/count-specific-character-occurrences-in -string) – 2014-08-30 10:51:42

+2

我喜歡當你可以通過在Google上粘貼問題標題並點擊第一個鏈接來找到解決方案... – 2014-08-30 10:52:43

回答

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

+0

你可能希望改變'If c =「」Then' to'If c = 「」然後「。我不確定會用Option Strict On進行編譯。 – 2014-09-02 15:50:21

0

您可以按照您的需要按排序方式進行操作。

嘗試

Dim count As Integer = 0 
textbox1.text = " 1 2 3 4 5 6 7 a b c" 
count = textbox1.text.Split(" ").Length -1 
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) 
[字符串計數特定的字符出現](的
+1

描述一下你在做什麼,以及這將如何幫助解決問題 – Abhijeetk431 2018-01-03 06:52:39

相關問題