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

问题描述

这是示例文本文件



行按日期分类,每行日期可以重复,例如,12月1日和2日有两个条目。预期的产出应该鼓舞人心的D例如每日期

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

这就是我目前所拥有的
使用(StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream))
{
$ $ $ {

(!stRead.EndOfStream )
{
var readedLine = stRead.ReadLine(); (int j = 01; j ($!


$ b if(!string.IsNullOrWhiteSpace(readedLine))
{

{
int readedLineTime = Convert.ToInt32(readedLine.Substring(09,02));
if(readedLineTime == j)
{

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

textfileOutput + = readedLine.Substring(0,11)+ - + countingChars + 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 [] {'\\\
'},StringSplitOptions.RemoveEmptyEntries).Length.ToString();
// Label2.Text =文件名;





$ b

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

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


解决方案

让我知道这是否有效。

 字典< string,int> dMyobject = new Dictionary< string,int>(); $(!!
$ b 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;);
countingChars = collection.Count;

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



$ b $ p
$ b

您需要在某些集合中推送这些值。所以你可以检查它。


$ b

后来,为了将这些值用于打印或任何其他用途,请使用以下内容:

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


This is the sample textfile

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 couonting the patter "D;" for example per date

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

This is what I currently have

 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
解决方案

Let me know if this works.

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#计数每组的字符串/字符串出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:39