比较两个列表< string>在c#中找到重复项

发布于 2025-01-30 01:56:48 字数 1360 浏览 2 评论 0原文

我在这里尝试了这些方法:

以上都没有解决我的问题。

我有两个列表,List1和List2。 List2是一个.txt文件读取到字符串中,然后使用逗号划分为列表。 List1是使用“ - ”浏览器句柄拆分的输出。因此,例如,List1具有“堆栈溢出”,“个人”,“ Microsoft Edge”,List2具有“个人”和“ Microsoft Edge”的项目。因此,这两个列表之间存在重叠。我已经尝试了以下方法,但没有成功。

List<string> duplicates = List2.Intersect(List1).ToList();
if (duplicates.Count > 0)
{
    foreach(var dup in duplicates)
    {
        Console.WriteLine(dup);
    }
}
else
{
    Console.WriteLine("No");
}

即使我认为Intersect会找到任何形式的重复项并将其列入列表,但这总是会丢掉。需要明确的是,我希望结果“个人”和“ Microsoft Edge”。我还尝试过

bool match = List1.Contains(TextDoc);
if (match)
{
    Console.WriteLine("Match");
}
else
{
    Console.WriteLine("no Match");
}

TextDoc只是使用file.readallText读取的txt文档。这些选项都没有给我一个匹配,即使据我所知,这两个选项都应该给出列表的上下文。 我在这里错过了绝对明显的东西吗?

I've tried the methods here:

None of the above solved my issue.

I have two lists, List1, and List2. List2 is a .txt file read into a string and then split into a list using commas. List1 is the output of a browser handle split using '-'. So, for example, List1 has the items "Stack Overflow", "Personal", "Microsoft Edge", and List2 has the items "Personal", and "Microsoft Edge". So, there is an overlap between the two lists. I've tried the methods below, with no success.

List<string> duplicates = List2.Intersect(List1).ToList();
if (duplicates.Count > 0)
{
    foreach(var dup in duplicates)
    {
        Console.WriteLine(dup);
    }
}
else
{
    Console.WriteLine("No");
}

This always will throw a no, even though I thought intersect would find ANY sort of duplicates and give it to the list. To be clear, I am hoping for the outcome "Personal" and "Microsoft Edge". I've also tried

bool match = List1.Contains(TextDoc);
if (match)
{
    Console.WriteLine("Match");
}
else
{
    Console.WriteLine("no Match");
}

TextDoc is just the txt document read into a string using File.ReadAllText. Neither of these options gives me a match, even though as far as I can tell both of them should given the context of the Lists.
Am I missing something super obvious here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2025-02-06 01:56:48

即使我认为Intersect会找到任何形式的重复项,并将其提供给列表

您的想法是正确的。

我在这里错过了一些非常明显的东西吗?

您缺少简单而简单的结论:您假设 list1 list2 之间存在一些重复的字符串只是不正确的。

在调试器中仔细检查两个列表中的字符串 。不要相信您对列表中哪些字符串的假设。您已经有证据表明,当调用Intersect方法时,这两个列表中都没有重复。验证非常仔细的在调用Intersect方法的确切时刻,这两个列表中的内容是什么。

附带说明:还要记住,有一些Unicode控制字符是看不见的(但是检查调试器中的字符串应揭示此类控制字符),并且有不同的Unicode字符看起来相似,即使不是相同(取决于字体)使用)尽管是不同的字符。而且您的字符串可能具有一个或另一个,使它们看起来相同,但实际上包含不同的角色序列,因此实质上使它们成为不平等的字符串。

even though I thought intersect would find ANY sort of duplicates and give it to the list

Your thought is correct.

Am I missing something super obvious here?

You are missing to draw the straightforward and simple conclusion: Your assumption that there are some duplicate strings between List1 and List2 is simply incorrect.

Inspect the strings in both lists carefully in the debugger. Do not trust your assumptions about what strings are in the list. You already got the evidence that there are no duplicates in both lists when the Intersect method is being invoked. Verify very carefully what is in both lists at the exact moment the Intersect method is being invoked.

As a side note: Also keep in mind that there are Unicode control characters that are invisible (but inspecting the strings in the debugger should reveal such control characters), and there are different Unicode characters that look similar if not identical (depending on the font used) despite being different characters. And your strings might perhaps feature one or the other, making them look the same but actually containing different character sequences, thus essentially making them unequal strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文