Results 1 to 6 of 6

Thread: How to count characters in RichTextBox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    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!!!

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    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.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

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

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: How to count characters in RichTextBox?

    I do thanks!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width