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!!!
Re: How to count characters in RichTextBox?
Do you want to get the only the charcters in the richtextbox ?
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
}
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]);
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. :D
Re: How to count characters in RichTextBox?