0

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

spaces = 10 , char = 10

J...
  • 30,968
  • 6
  • 66
  • 143
user3418036
  • 103
  • 2
  • 4
  • 14
  • 2
    possible duplicate of [Count specific character occurrences in string](http://stackoverflow.com/questions/5193893/count-specific-character-occurrences-in-string) – J... Aug 30 '14 at 10:51
  • 2
    I love when you can find the solution by just pasting the title of your question in Google and click on the first link... – gilles emmanuel Aug 30 '14 at 10:52

3 Answers3

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 will iterate each character within the string then it will check whether c is a space or not. if it is a space then increment the spaceCount else increment the lettrtCount

  • You may wish to change `If c = " " Then` to `If c = " "c Then`. I'm not certain that will compile with Option Strict On. – Chris Dunaway Sep 02 '14 at 15:50
0

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
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
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)
Bugs
  • 4,491
  • 9
  • 32
  • 41