By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Count the number of characters in a string

P: n/a
Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck."

******
How would I go about counting the number of characters in this string
without including the commas, semicolons, spaces, and periods.

Would like to display the number result in a label.

Any suggestions would be appreciated.

bebop

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
2 words: Regular Expressions. :)

Something along the lines of:

Match m = Regex.Match(st, "[a-z][A-Z][0-9]");
if(m.Match)
{
Debug.WriteLine(m.Length);
}

(I didn't test the code, so you may have to play with the regex expression.)

HTH,
~d

cw bebop wrote:
Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck."

******
How would I go about counting the number of characters in this string
without including the commas, semicolons, spaces, and periods.

Would like to display the number result in a label.

Any suggestions would be appreciated.

bebop

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2

P: n/a
cw bebop <cw****@yahoo.com> wrote in
news:eY**************@tk2msftngp13.phx.gbl:
Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink
this tea. Run like heck."

******
How would I go about counting the number of characters in this
string without including the commas, semicolons, spaces, and
periods.

Would like to display the number result in a label.


bebop,

A regular expression can be used to capture the number of
letters in the string:
using System.Text.RegularExpressions;

....

string st = "Hi, these pretzels are making me thirsty; drink this tea. Run like heck.";
this.Label1.Text = Regex.Matches(st, "[a-zA-Z]").Count.ToString();
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 17 '05 #3

P: n/a
Please go through Regex members, you will fine what you need. One
alternate suggestion is if you have set of charaters you want to ignore
during the count, use replace verb in the Regex.

Nov 17 '05 #4

P: n/a
And if you really want to get ugly...

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck.";

int count = st.Replace(",","").Replace(";","").Replace("
","").Replace(".","").Length;

(lol, sorry, I couldn't resist!)

~d

cw bebop wrote:
Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck."

******
How would I go about counting the number of characters in this string
without including the commas, semicolons, spaces, and periods.

Would like to display the number result in a label.

Any suggestions would be appreciated.

bebop

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5

P: n/a
Hi DotNet Coder

That works great. Is there a method in c# to count the number of words
in a string?

string st = "something is there. It's a good day,"

result would be 7

bebop

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6

P: n/a

Hi Chris

That works great. Is there a method in C# to count the number of words
in a string?

string st = "something is there. It's a good day."

result would be 7

bebop
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7

P: n/a

DotNet Coder

kept receiving the error:

String cannot be of zero length. Parameter name: oldValue

when running the code

int count = st.Replace(",","").Replace(";","").Replace("
","").Replace(".","").Length;

Is there a way to fix this?

bebop
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8

P: n/a
Oy vey... don't use this example.. it's a coding faux paux and was just
a joke (even though it does work... lol)

Yes, if you are sure of the delimiter that you are using to seperate the
words, you can use:

string st = "something is there. It's a good day,";
string[] words = st.Split(new char[] { char.Parse(" ") });
Debug.WriteLine(words.Length);

Should be 7.

Or, you can also use Regular Expressions, which is the route I would
recommend because it splits on any white-space character:

MatchCollection mc = Regex.Matches("something is there. It's a good
day,", @"((^|\s)\S)");
System.Diagnostics.Debug.WriteLine(mc.Count);

Should also return 7.
HTH,
~d

cw bebop wrote:
Hi DotNet Coder

That works great. Is there a method in c# to count the number of words
in a string?

string st = "something is there. It's a good day,"

result would be 7

bebop

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #9

P: n/a
Yeah, use Regular Expressions. :-D

Actually, it ran fine for me...

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck.";

int count = st.Replace(",","").Replace(";","").Replace("
","").Replace(".","").Length;

System.Diagnostics.Debug.WriteLine(count.ToString( ));
cw bebop wrote:
DotNet Coder

kept receiving the error:

String cannot be of zero length. Parameter name: oldValue

when running the code

int count = st.Replace(",","").Replace(";","").Replace("
","").Replace(".","").Length;

Is there a way to fix this?

bebop
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.