删除多元素字符串[]数组中的重复项?

时间:2011-02-14 21:24:43

标签: c# .net arrays .net-4.0

我有一个由\r拆分的字符串[]数组。数组中的每一行都有标题地址,但每隔一段时间我最终会得到一个副本的地址部分,这是我不想要的。

此:

Title1 | Address1 //[0]
Title2 | Address2 //[1]
Title3 | Address1 //[2]
Title4 | Address3 //[3]

会变成:

Title1 | Address1 //[0]
Title2 | Address2 //[1]
Title4 | Address3 //[2]

数组声明如下:string[] resultsArray = results.Split('\r'); //Title|Address然后,当我按|抓取单个元素时,我会拆分行。

用法(极简化):

foreach (string result in resultsArray)
{
    string splitResult[] = result.Split('|');
    title = splitResult[0];
    address = splitResult[1];
}

5 个答案:

答案 0 :(得分:5)

我假设问题是,如何防止重复地址输入列表。你能用Dictionary吗?

Dictionary<string, string> addresses = new Dictionary<string, string>();

foreach(string result in resultsArray)
{
    string splitResult[] = result.Split('|');

    // check to see if address already exists, if it does, skip it.
    if(!addresses.ContainsKey(splitResult[1]))
    {
        addresses.add(splitResult[1], splitResult[0]);
    }
}   

答案 1 :(得分:3)

string[] strings = { "Title1 | Address1", "Title2 | Address2", "Title3 | Address1", "Title4 | Address3" };
var _strings = strings.GroupBy(s => s.Split('|')[1]).Select(g => g.Min(s => s));

答案 2 :(得分:2)

var seenItBefore = new HashSet<string>();
foreach (string result in resultsArray)
{
    string splitResult[] = result.Split('|');
    title = splitResult[0];
    address = splitResult[1];

    if (!seenItBefore.Add(address)) continue;

    // process
}

如果您正在构建IEqualityComparer<string[]>的藤蔓,您还可以提供Distinct()IEnumerable<>,但由于您的样本没有使用它,我决定坚持使用经典程序。

答案 3 :(得分:1)

使用John Skeet的ProjectionComparer,它变得相当容易:

var comparer = new ProjectionComparer((string input) => input.Split('|')[1]);
var results = resultsArray.Distinct(comparer);

答案 4 :(得分:0)

尝试类似:

resultArray.Select(p => p.Split('|')).Select(p => new { Name = p[0], Address = p[1] }).GroupBy(p => p.Address).Select(p => p.First()).ToArray();