|
-
Sep 4th, 2007, 06:44 AM
#1
Thread Starter
Junior Member
How to count characters in RichTextBox?
Any one knows how to make statistic from richtextbox counting characters in detail, like a=20, b=7 etc. I know to find dhe absolute number of characters with function "richTextBox1.TextLength" but to access every character i'm not aible!!!
Please help!!!
-
Sep 4th, 2007, 10:30 AM
#2
Re: How to count characters in RichTextBox?
Do you want to get the only the charcters in the richtextbox ?
Please mark you thread resolved using the Thread Tools as shown
-
Sep 4th, 2007, 02:30 PM
#3
Junior Member
Re: How to count characters in RichTextBox?
I don't understand exactly what you want...(I don't know of any system where a=20 and b=7.)if you want to count the number of characters, use richTextBox1.Text.Length, it's an int value.
If you want to analyze every character and do something with it, you can use a foreach loop:
Code:
foreach (Char c in richTextBox1.Text.ToCharArray())
{
MessageBox.Show(c.ToString()); //do whatever you want with "c" it will represent the current Char value
}
Last edited by gamesguru; Sep 4th, 2007 at 02:34 PM.
-
Sep 4th, 2007, 03:20 PM
#4
Re: How to count characters in RichTextBox?
Awkward way:
Code:
int[] char_counts = new int[26];
char[] chars = myrtb.Text.ToLower().ToCharArray();
// count characters:
foreach (char c in chars)
++char_counts[(int)c - (int)'a'];
// display them:
for (int i = 0; i <= 26; ++i)
Console.WriteLine((char)(i + 96) + ": " + char_counts[i]);
-
Sep 4th, 2007, 04:08 PM
#5
Re: How to count characters in RichTextBox?
Awesome way (but probably slower):
Code:
char[] chars = myRTB.Text.ToLower().ToCharArray();
int[] char_counts = new int[26];
for (int i = 0; i < char_counts.Length; ++i)
char_counts[i] = Array.FindAll<char>(chars, delegate(char c) {
return c == (char)(i + (int)'a'); }
).Length;
edit: actually, that would be a lot slower, because it has to make 26 passes over the string, as opposed to one. I just wanted an excuse to use predicates.
-
Sep 11th, 2007, 03:08 AM
#6
Thread Starter
Junior Member
Re: How to count characters in RichTextBox?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|