How many spaces in a textbox?

Is there any easy way to determine how many spaces are in a textbox?  Or, how many non-space characters are in a textbox?

At any given time, the text box will have 5 characters, such as "AB   ", or "012  ", and I ultimately need to determine how many non-space characters there are--preferably without a lot of complex code.  Any suggestions?  TIA
Who is Participating?
Kinger247Connect With a Mentor Commented:
Try nice quick:

Dim CharsSpaces as integer = TextBox1.text.Split(" ").GetUpperBound(0)
newyuppieConnect With a Mentor Commented:
try maybe:

        Dim i As Integer

        For Each c As Char In TextBox1.Text.ToCharArray
            If Char.IsSeparator(c) Then
                i += 1
            End If

        MessageBox.Show("Spaces: " & i)
Fernando SotoConnect With a Mentor RetiredCommented:
Hi sasllc;

Is this simple enough.

        Dim input As String = "AB   "     ' Test Data
        Dim strLen As Integer = 0         ' Number of chars in string

        strLen = input.Replace(" "c, String.Empty).Length

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

simple not always means SINGLE LINED....
it should also be understandable and readable by a beginner.
Fernando SotoRetiredCommented:

And what is not understandable about replacing all spaces with an empty string and getting the length like the below line?

        input.Replace(" "c, String.Empty).Length

hi fernando,
well what i meant is that it may be the case that beginner programmers dont know or dont wish to know how a particular piece of code works, as long as its doing the job. your code is very simple and clever, im not saying its not. but why assume that everybody would know that if you code " "c it means a char? in fact I didnt know until last week browsing the questions!
Fernando SotoRetiredCommented:
Hi NY;

I do not assume that what I post is understandable to everyone who reads it. I do hope that they at least try the code and for those parts that they do not understand to ask a follow up question to my post. I am always willing to explain my answers and to help others understand what I have done. If we do not try new things then how are we to grow in the profession we have selected. As you stated that you read other post and pick up new things that you did not know so I hope that someone else will read this post and pick up new things.

You have a great day;

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.