0

次のような行があるとします。

イングランドAチームプレーヤー:フリントフ
イングランドAチームプレーヤー:フリントフ
イングランドBチームプレーヤー:シュトラウス
イングランドAチームプレーヤー:シュトラウス
インドチームプレーヤーA:サチンテンドルカール
インドチームプレーヤーB:サチンテンドルカール
インドチームプレーヤーA:ジャバガルスリナート

今私が欲しいのは、ユニークな値のカウントを検索して返すことです。たとえば、イングランドのプレーヤーのユニークなカウントを検索したい場合は、上記の例のように2が表示されます。

私が試したコードですが、機能していません:

string searchKeyword = "England";
string fileName = @"C:\Users\karansha\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();
foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        results.Add(line);
    }
}
List<string> users = new List<string>();
Regex regex = new Regex(@"player:\s*(?<playername>.*?)\s+appGUID");
MatchCollection matches = regex.Matches(searchKeyword);
foreach (Match match in matches)
{
    var user = match.Groups["username"].Value;
    if  (!users.Contains(user)) users.Add(user);
}
int numberOfUsers = users.Count;
Console.WriteLine(numberOfUsers);
// keep screen from going away
// when run from VS.NET
Console.ReadLine();
4

4 に答える 4

1

より簡単な方法は、LINQ を使用することです。

string searchKeyword = "England";
string fileName = @"C:\Users\renan.stigliani\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);

int numberOfUsers = textLines
                        .Where(x => x.Contains(searchKeyword))
                        .Distinct()
                        .Count();

Console.WriteLine(numberOfUsers);

// keep screen from going away
// when run from VS.NET
Console.ReadLine();

@DominicKexelが指摘したように、私はforeachを一掃しました

于 2013-03-01T13:41:20.190 に答える
0

少し単純に聞こえるかもしれませんが、リストから区別して選択してみませんか?例:

string searchKeyword = "England";
string fileName = @"C:\Users\karansha\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();
foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        results.Add(line);
    }
}
List<string> users = results.Distinct().toList();

それはあなたにユニークな線を与えるでしょう、そしてあなたは分割する必要があります、あなたはそれを簡単に行うことができます。カウントでユニークな数を知ることができます。

于 2013-03-01T13:43:27.700 に答える
0

正規表現のアプローチに慣れていない場合は、値( "England player:Foo"など)をDictionaryオブジェクトに追加し、重複が追加されないように値をキーにしてから、Countメソッドを使用できます。containsKeyメソッドも参照してください。

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

于 2013-03-01T13:44:07.723 に答える
0
  1. リストにすでにキーワードが含まれているかどうかを確認する必要があります
  2. 辞書コレクションを使用します。
  3. その正規表現は必要ありません。

string searchKeyword = "England";
string fileName = @"C:\Users\karansha\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);
Dictionary<string,int> results = new Dictionary<string,int>;
foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        if(results.Keys.Any(searchKeyword))
        {
            results[searchKeyword]++;
        }
        else
        {
            results.Add(searchKeyword,1);
        }
        results.Add(line);
    }
}

foreach(var item in results)
{
    Console.WriteLine("Team:"+item.Key +"\nPlayer Count:"+item.Value);
}
于 2013-03-01T13:44:09.930 に答える