在ASP.Net中使用C#计算每个组的模式/字符串出现次数 [英] Count Pattern / String Occurrence Per Group using C# in ASP.Net

查看:48
本文介绍了在ASP.Net中使用C#计算每个组的模式/字符串出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例文本文件

按日期对行进行分类,每个行的日期可以重复,例如12月1日和2日有两个条目. 预期的产量应该计算模式"D;"例如每个日期

Line is categorized by date, per line date can be repeated like for example, December 1 and 2 have two entries. Expected Output should be counting the pattern "D;" for example per date

2016-12-01 - 7 
2016-12-02 - 9
2016-12-03 - 5

这是我目前拥有的

 using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream))
        {

            while (!stRead.EndOfStream)
            {
                var readedLine = stRead.ReadLine();

                if (!string.IsNullOrWhiteSpace(readedLine))
                {

                    for (int j = 01; j <= 31; j++)
                    {
                        int readedLineTime = Convert.ToInt32(readedLine.Substring(09, 02));
                        if (readedLineTime == j)
                        {

                            MatchCollection collection = Regex.Matches(readedLine, @"D;");
                            countedChars = collection.Count;

                            textfileOutput += readedLine.Substring(0, 11) + " - " + countedChars + Environment.NewLine;

                        }
                    }
                }
                textfileContent += readedLine + Environment.NewLine;
                i++;
            }

            TextBox1.Text = textfileOutput;
            TextBox2.Text = textfileContent;
            Label1.Text = i.ToString();
            //Label1.Text =  this.TextBox1.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString();
          //  Label2.Text = filename;

        }

这是它的当前输出,它显示在多行文本框中

and this is its current output that is being displayed in multiple line textbox

2016-12-01  - 4
2016-12-01  - 3
2016-12-02  - 4
2016-12-02  - 5
2016-12-03  - 5

推荐答案

让我知道是否可行.

Dictionary<string, int> dMyobject = new Dictionary<string, int>();

while (!stRead.EndOfStream)
            {
                var readedLine = stRead.ReadLine();

                if (!string.IsNullOrWhiteSpace(readedLine))
                {

                    int readedLineTime = Convert.ToInt32(readedLine.Substring(09, 02));
                    string sDate = readedLine.Substring(0, 11);

                    MatchCollection collection = Regex.Matches(readedLine, @"D;");
                    countedChars = collection.Count;

                    if (!dMyobject.Keys.Contains(sDate))
                    {
                        dMyobject.Add(sDate, collection.Count);
                    }
                    else
                    {
                        dMyobject[sDate] = dMyobject[sDate] + collection.Count;
                    }
                }
                textfileContent += readedLine + Environment.NewLine;
                i++;
            }

您将需要将这些值推送到某些集合中.这样您就可以检查它.

You will need to push these values in some collection. So that you can check it.

以后,要使用这些值进行打印或其他操作,请使用以下

Later, for using these values for printing or anything, use following

foreach (var item in dMyobject)
            {
                Console.WriteLine(item.Key + "  " + item.Value);
            }

这篇关于在ASP.Net中使用C#计算每个组的模式/字符串出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆