比较两个字符串以查找任何重复项

发布于 2025-01-14 06:11:05 字数 541 浏览 2 评论 0原文

我真的找不到这个问题的答案,这让我精神崩溃。我有两个字符串,一个是一个文本文件,它被读入一个名为 logfile 的字符串中。另一个只是用户输入的字符串,称为text1。最终这只是一个有提示的猜谜游戏,但我不知道如何比较这两者的相等性。

string LOG_PATH = "E:\\Users\\start.txt";
string logfile = File.ReadAllText(LOG_PATH);
string text1 = "";
text1 = Console.ReadLine();

if (logfile.Contains(text1))
{
    Console.WriteLine("found");
}
else
{
    Console.WriteLine("not found");
}

当文本文件中只有一个单词并且匹配时,此代码可以正常工作。如果文本文件仅包含单词“Mostly”并且用户输入了most 和一堆其他单词,则控制台将打印找到。但是,如果文本文件包含大部分和一堆其他随机单词,例如“今天大部分多云”,控制台将打印未找到。是否可以匹配任何重复的字符串?

I really cannot find the answer to this question and it is driving me mental. I have two strings, one is a text file that is read into a string called logfile. The other is just a user input string, called text1. Eventually it's just going to be a guessing game with hints, but I can't figure out how to compare these two for equality.

string LOG_PATH = "E:\\Users\\start.txt";
string logfile = File.ReadAllText(LOG_PATH);
string text1 = "";
text1 = Console.ReadLine();

if (logfile.Contains(text1))
{
    Console.WriteLine("found");
}
else
{
    Console.WriteLine("not found");
}

This code works fine when there is only one word in the text file and matches. If the text file only contains the word "Mostly" and the user entered mostly and a bunch of other words, the console prints found. But if the text file has mostly and a bunch of other random words, say "Mostly cloudy today", the console prints not found. Is it possible to match to strings for ANY duplicates at all?

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

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

发布评论

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

评论(2

﹏雨一样淡蓝的深情 2025-01-21 06:11:05

您可以通过不同的方式尝试,

使用 Except()

var wordsFromFile = File.ReadAllText(LOG_PATH).Split(' ').ToList();
var inputWords = Console.ReadLine().Split(' ').ToList();

Console.WriteLine(wordsFromFile.Except(inputWords).Any() ? "Found" : "Not Found");

类似使用foreach()循环的方式,

var wordsFromFile = File.ReadAllText(LOG_PATH).Split(' ').ToList();
var inputWords = Console.ReadLine();
string result = "Not Found";

foreach(var word in inputWords)
{
      if(wordsFromFile.Contains(word))
      {
          result = "Found";
          break
      }
}
Console.WriteLine(result);

You can try it with different ways,

Using Except(),

var wordsFromFile = File.ReadAllText(LOG_PATH).Split(' ').ToList();
var inputWords = Console.ReadLine().Split(' ').ToList();

Console.WriteLine(wordsFromFile.Except(inputWords).Any() ? "Found" : "Not Found");

Similar way using foreach() loop,

var wordsFromFile = File.ReadAllText(LOG_PATH).Split(' ').ToList();
var inputWords = Console.ReadLine();
string result = "Not Found";

foreach(var word in inputWords)
{
      if(wordsFromFile.Contains(word))
      {
          result = "Found";
          break
      }
}
Console.WriteLine(result);
巴黎盛开的樱花 2025-01-21 06:11:05

与 Prasad 所做的非常相似,只是我们忽略空行并使用不区分大小写的比较:

string LOG_PATH = @"E:\Users\start.txt";
List<String> logfileWords = new List<String>();
foreach (String line in File.ReadLines(LOG_PATH))
{
    logfileWords.AddRange(line.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
}

Console.Write("Words to search for (separated by spaces): ");
String[] inputs = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("Inputs:");
foreach(String input in inputs)
{
    Console.WriteLine(input + " ==> " + (logfileWords.Any(w => w.Equals(input, StringComparison.InvariantCultureIgnoreCase)) ? "found" : "not found"));
}

Very similar to what Prasad did, except we ignore blank lines and use a case-insensitive comparison:

string LOG_PATH = @"E:\Users\start.txt";
List<String> logfileWords = new List<String>();
foreach (String line in File.ReadLines(LOG_PATH))
{
    logfileWords.AddRange(line.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
}

Console.Write("Words to search for (separated by spaces): ");
String[] inputs = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("Inputs:");
foreach(String input in inputs)
{
    Console.WriteLine(input + " ==> " + (logfileWords.Any(w => w.Equals(input, StringComparison.InvariantCultureIgnoreCase)) ? "found" : "not found"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文