比较两个字符串以查找任何重复项
我真的找不到这个问题的答案,这让我精神崩溃。我有两个字符串,一个是一个文本文件,它被读入一个名为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过不同的方式尝试,
使用
Except()
,类似使用
foreach()
循环的方式,You can try it with different ways,
Using
Except()
,Similar way using
foreach()
loop,与 Prasad 所做的非常相似,只是我们忽略空行并使用不区分大小写的比较:
Very similar to what Prasad did, except we ignore blank lines and use a case-insensitive comparison: