count the number of characters in a string

MiPakTeh 1,476 Reputation points
2022-01-30T12:59:51.007+00:00

Hi All,

I want to count occurance of number in string.All the in the "Notepad" file. "C:\Users\family\Documents\Tool.txt".

7916,1465,8157,8772,5279,8812,8665,9345,9341,6727
0394,2309,9371,9343,8240,8149,2140,4515,7396,7356

Put to the List Data1.Then split (',')
Put again into List Data2.When I use the listbox to show the List Data2, it look like
7916
1465
8157
8772
I need Data2 show like bellow then count.
7916146581578772527988128665934593416727
0394230993719343824081492140451573967356

Then count Occurance number 0 to 9

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace TestFor_8  
{  
    public partial class Form1 : Form  
    {  
        List<string> Data1 = new List<string>();  
        List<string> Data2 = new List<string>();  
        List<string> Data3 = new List<string>();  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\family\Documents\Tool.txt");  
  
            foreach (var vNum in lines)  
            {  
                listBox1.Items.Add(vNum);  
            }  
        }  
  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
            string[] separatingStrings = { "<<", "...", "," };  
            char[] charSeparators = new char[] { ',', ' ' };  
  
            Data1.AddRange(File.ReadAllLines(@"C:\Users\family\Documents\Tool.txt"));  
  
            for (int i = 0; i < Data1.Count; i++)  
            {  
                listBox1.Items.Add(Data1[i]);  
                String StringToCheck = Data1[i].ToString();  
                String[] StringsToCheck = StringToCheck.Split(charSeparators);  
                Data2.AddRange(StringsToCheck);  
            }  
  
        }  
  
        private void button3_Click(object sender, EventArgs e)  
        {  
            for (int j = 0; j <=9 ; j++)  
            {  
                String Number_ = j.ToString();  
                int NumberCount_ = Data3[j].ToCharArray().Count(c => c == Number_);  
                listBox3.Items.Add(NumberCount_);  
  
            }  
  
  
        }  
    }  
}  








  
  
Developer technologies C#
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-01-30T13:42:27.517+00:00

    Here is go

    169681-figure1.png

    string values =   
        "791614658157877252798812866593459341672" +   
        "70394230993719343824081492140451573967356";  
      
    var characterGroup = (  
            from chr in values.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();  
    

    Or for in a class

    Class

    public class Item  
    {  
        public char Letter { get; set; }  
        public int Occurrences { get; set; }  
        public int Code { get; set; }  
         
    }  
    

    Worker class

    public class Operations  
    {  
        public static List<Item> GetItems(string values)  
        {  
            var characterGroup = (  
                    from chr in values.ToCharArray()  
                    group chr by chr into grp  
                    select new Item { Letter = grp.Key, Occurrences = grp.Count(), Code = Convert.ToInt32((int)grp.Key) })  
                .ToList()  
                .OrderBy((item) => item.Letter.ToString());  
      
            return (from item in characterGroup select item).ToList();  
              
        }  
    

    Usage

    private void CountButton_Click(object sender, EventArgs e)  
    {  
        string values =   
            "791614658157877252798812866593459341672" +   
            "70394230993719343824081492140451573967356";  
      
        dataGridView1.DataSource = Operations.GetItems(values);  
    }  
    

    In a ListBox

    public class Item  
    {  
        public char Letter { get; set; }  
        public int Occurrences { get; set; }  
        public int Code { get; set; }  
        public override string ToString() => $"{Letter} - {Occurrences}";  
    }  
    

    Usage

    private void CountButton_Click(object sender, EventArgs e)  
    {  
        string values =   
            "791614658157877252798812866593459341672" +   
            "70394230993719343824081492140451573967356";  
        listBox1.DataSource = Operations.GetItems(values);  
      
    }  
    

    169663-f1.png

    Edit, to get a string from comma and returns

        var raw = @"7916,1465,8157,8772,5279,8812,8665,9345,9341,6727  
    0394,2309,9371,9343,8240,8149,2140,4515,7396,7356";  
        string values = raw.Replace(",", "").Replace("\r\n", "");  
        listBox1.DataSource = Operations.GetItems(values);  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-01-31T05:42:20.413+00:00

    @MiPakTeh , based on your description, you want to count the number of characters in a string from the txt file.

    Here is a code example you could refer to.

     public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            List<string> Data1 = new List<string>();  
            List<string> Data2 = new List<string>();  
            List<string> Data3 = new List<string>();  
            private void button1_Click(object sender, EventArgs e)  
            {  
                string[] lines = System.IO.File.ReadAllLines(@"C:\Users\username\Desktop\test1.txt");  
      
                foreach (var vNum in lines)  
                {  
                    listBox1.Items.Add(vNum);  
                    Data1.Add(vNum);  
                }  
            }  
      
            private void button2_Click(object sender, EventArgs e)  
            {  
                foreach (var item in Data1)  
                {  
                    string t = item.Replace(",","").Trim();  
                    Data2.Add(t);  
                }  
                foreach (var item in Data2)  
                {  
                    listBox2.Items.Add(item);  
                }  
            }  
      
            private void button3_Click(object sender, EventArgs e)  
            {  
                foreach (var item in Data2)  
                {  
                    for (int j = 0; j <= 9; j++)  
                    {  
                        String Number_ = j.ToString();  
                        int NumberCount_ = item.ToCharArray().Count(c => c.ToString() == Number_);  
                        //int NumberCount_ = Data2[0].Count(c => c.ToString() == Number_);  
                        listBox3.Items.Add(j.ToString() + "-" + NumberCount_);  
                    }  
                    listBox3.Items.Add("************************");  
                }  
                  
      
            }  
        }  
    

    If you click button 1 button 2 button 3 in turn, you will get the following result:

    169746-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.