browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need C# / C Sharp help?

Get answers from our community of C# / C Sharp experts on BYTES! It's free.

Count the number of characters in a string

cw bebop
Guest
 
Posts: n/a
#1: Nov 17 '05
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 ***



DotNet Coder
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Count the number of characters in a string


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:[color=blue]
> 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 ***[/color]
Chris R. Timmons
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Count the number of characters in a string


cw bebop <cwbp15@yahoo.com> wrote in
news:eYRaoeeQFHA.2520@tk2msftngp13.phx.gbl:
[color=blue]
> 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.[/color]

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/
DBC User
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Count the number of characters in a string


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.

DotNet Coder
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Count the number of characters in a string


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:[color=blue]
> 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 ***[/color]
cw bebop
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Count the number of characters in a string


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 ***
cw bebop
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Count the number of characters in a string



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 ***
cw bebop
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Count the number of characters in a string



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 ***
DotNet Coder
Guest
 
Posts: n/a
#9: Nov 17 '05

re: Count the number of characters in a string


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:[color=blue]
> 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 ***[/color]
DotNet Coder
Guest
 
Posts: n/a
#10: Nov 17 '05

re: Count the number of characters in a string


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:[color=blue]
> 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 ***[/color]
Closed Thread


Similar C# / C Sharp bytes