Results 1 to 3 of 3

Thread: Count character Occurrences in a string

Threaded View

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Count character Occurrences in a string

    I have seen this question many times so thought it would be good to post the following code. There are two versions of the code as per below.

    Code:
    using System;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CountCharacters
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            /// <summary>
            /// With encoding
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                //string items = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";
    
                Encoding iso = Encoding.GetEncoding("ISO-8859-1");
                Encoding utf8 = Encoding.UTF8;
                byte[] utfBytes = utf8.GetBytes(textBox1.Text);
                byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
                string itemsTranslated = iso.GetString(isoBytes);
    
                var characterGroup = (
                    from chr in itemsTranslated.ToCharArray()
                    group chr by chr into grp
                    select new
                    {
                        Letter = grp.Key,
                        Occurrences = grp.Count(),
                        Code = Convert.ToInt32((int)grp.Key)
                    })
                    .ToList()
                    .OrderBy((item) => item.Letter.ToString());
    
                var results = (from item in characterGroup select item).ToList();
    
                dataGridView1.DataSource = results;
            }
    
            /// <summary>
            /// Without encoding
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                var characterGroup = (
                    from chr in textBox1.Text.ToCharArray()
                    group chr by chr into grp
                    select new
                    {
                        Letter = grp.Key,
                        Occurrences = grp.Count(),
                        Code = Convert.ToInt32((int)grp.Key)
                    })
                    .ToList()
                    .OrderBy((item) => item.Letter.ToString());
    
                dataGridView1.DataSource = (from item in characterGroup select item).ToList();
    
            }
        }
    }
    Full source in VS2013

    Name:  31.jpg
Views: 1471
Size:  41.0 KB
    Attached Images Attached Images  

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