Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How many spaces in a textbox?

Posted on 2006-11-10
Medium Priority
208 Views
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
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
• 4
• 3

LVL 13

Assisted Solution

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

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

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

LVL 64

Assisted Solution

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

LVL 13

Expert Comment

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

LVL 64

Expert Comment

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

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 64

Expert Comment

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

ID: 17921400
agreed!
NY
0

## Featured Post

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month10 days, 9 hours left to enroll