Count the number of characters in a string

C

cw bebop

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
 
D

DotNet Coder

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
 
C

Chris R. Timmons

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();
 
D

DBC User

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.
 
D

DotNet Coder

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
 
C

cw bebop

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
 
C

cw bebop

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
 
C

cw bebop

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
 
D

DotNet Coder

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
 
D

DotNet Coder

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());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top