?
Solved

How many spaces in a textbox?

Posted on 2006-11-10
8
Medium Priority
?
207 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:sasllc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Assisted Solution

by:newyuppie
newyuppie earned 400 total points
ID: 17913909
try maybe:

        Dim i As Integer

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

        MessageBox.Show("Spaces: " & i)
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 1200 total points
ID: 17913944
Try nice quick:

Dim CharsSpaces as integer = TextBox1.text.Split(" ").GetUpperBound(0)
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 total points
ID: 17914006
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

Fernando
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:newyuppie
ID: 17914025
simple not always means SINGLE LINED....
it should also be understandable and readable by a beginner.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17916220
@newyuppie

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

Fernando
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17917564
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!
cheers
NY
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17921376
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;

Fernando
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17921400
agreed!
NY
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question