2017-01-30 3 views
0

でこれはサンプルテキストファイルカウントパターン/文字列の出現ASP.Net

enter image description here

ラインがライン日付ごと、日付ごとに分類されるが、例えば、12月1日及び2のように繰り返すことができることです2つのエントリがあります。 期待される出力は、パターン "D;"を数える必要があります。日付ごとに例えば

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; 

     } 

を持っており、これは

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

リッキー次の印刷または何か、使用のためにこれらの値を使用するので、あなたはそれに対して「D」の出現の日付と番号をしたいです。それは正しい理解ですか? – A3006

+0

「D;」の出現を数えたいと思います。日付ごとに各行に入力し、例と同じように、同じ日付で数を数えたり組み合わせたりしてください。 – rickyProgrammer

+0

よろしいですか。そして、(int j = 01; j <= 31; j ++)のためのものは何ですか?また、あなたの入力ファイルは "csv"ですか? – A3006

答えて

1

が私に教えてください複数行のテキストボックスに表示されているその電流出力は何かということですこれが動作すれば。

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++; 
      } 

いくつかのコレクションでは、これらの値をプッシュする必要があります。あなたがそれを確認できるように。

その後、

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

それは良いコードではないことを認めなければなりません。前に尋ねたように、入力ファイルの種類がcsvの場合、コードを最適化することができます。私は入力の種類を知らなかったので、この答えを提供しました。 :-) – A3006

+0

inoutのタイプは単なるテキストファイルです – rickyProgrammer

+0

上記のものはあなたのために働くのですか? – A3006

0
string line = ""; 
     Dictionary<string, int> list = new Dictionary<string, int>(); 
     int count; 
     if (File.Exists(fileName) == true) 
     { 
      StreamReader objReader; 
      objReader = new StreamReader(fileName); 
      StreamWriter file = new StreamWriter(OutputfileName"); 
      do 
      { 
       line = objReader.ReadLine(); 
       string temp = line.Substring(0, 10); 
       if (!list.ContainsKey(temp)) 
       { 
        MatchCollection collection = Regex.Matches(line, @"D;"); 
        count = collection.Count; 
        list.Add(temp, count); 
       } 
       else 
       { 
        MatchCollection collection = Regex.Matches(line, @"D;"); 
        count = collection.Count; 
        var val = list[temp]; 
        list[temp] = count + val; 
       } 
      } while (objReader.Peek() != -1); 
      foreach (var j in list) 
      { 
       file.WriteLine(j.Key + " - " + j.Value+"\n"); 
      } 
      file.Close(); 
     } 
関連する問題