[英]check if string exists in a file
我有以下一段代码,它打开一个文本文件并读取文件中的所有行并将其存储到一个字符串数组中。
然后检查字符串是否存在于数组中。 然而,我面临的问题是,无论何时找到一个字符串,它总是显示“有匹配项”以及“没有匹配项”。 知道如何解决这个问题吗?
检查此代码:
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
}
}
if (sr != null)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
听起来过于复杂,如果您想知道文件中是否存在字符串,则没有理由按行或任何内容进行检查。 您可以简单地将所有代码替换为:
if(File.ReadAllText(path).Contains(domain))
{
MessageBox.Show("There is a match");
}
我建议按如下方式设置和标记并检查它...
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
bool isMatch = false;
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
isMatch = true;
}
}
if (!isMatch)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
祝你好运!
实际上,您不需要将整个文件读入内存。 有File.ReadLines方法,它允许您一一枚举文件行,而无需读取整个文件。 您可以创建以下方法
private bool DomainExists(string domain)
{
foreach(string line in File.ReadLines(path))
if (domain == line)
return true; // and stop reading lines
return false;
}
此方法的用法如下所示:
if (DomainExists(domain))
MessageBox.Show("there is a match");
else
MessageBox.Show("there is no match");
还有两个旁注 - 如果您使用File.ReadAllLines
阅读行(它在内部创建阅读器),则不需要StreamReader
。 只需检查 - 您甚至不在任何地方使用sr
变量。 第二个注意事项 - 如果将流包装在using
块中,则无需手动关闭流。 在这种情况下,流将被自动处理和关闭。
最简单的方法:
string content = File.ReadAllText(path);
if (content.IndexOf(domain) > -1)
{
// domain exists
}
else
{
// domain does not exist
}
现在来分析你的代码:
第一,您正在创建 StreamReader 实例,但稍后不会在代码中使用它。
2、如果域名在文件中多次出现怎么办? 在您的代码中,您将在代码中获得多个“有匹配项”。
using (StreamReader sr = File.OpenText(path)) // you can remove this line
{
string[] lines = File.ReadAllLines(path); // as you are not using it here
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
hasMatch = true;
break; // exit loop if found
}
}
if (!hasMatch)
{
// there is no match
}
if (sr != null) // you dont need this if you remove it from the beginning of the code
{
sr.Close();
MessageBox.Show("there is no match");
}
}
你可以试试这个:
首先,您需要创建一个接收字符串类型数组的方法,然后我们将该数组转换为字符串,然后我们从 txt 中读取所有文本,然后我们可以使用 ( Contains ) 来知道我们发送的文本是否存在于我们的txt,我们验证这是真是假,希望对您有所帮助。
public static void TextValidation(string[] val){
//Path of your file
string path = "/Users/Desktop/YourFile.txt";
//Array to string
string b = string.Join(",",val);
//Validate if exists
if(File.ReadAllText(path).Contains(b)){
Console.WriteLine("found");
// Do something if the data is found
}else{
Console.WriteLine("Not found");
}
}
由于接受的答案没有解决原始问题中的问题,这里是一个基于 LINQ 的简短版本:
private static bool TextFoundInFile(string fileName, string text)
{
// If the line contains the text, FirstOrDefault will return it.
// Null means we reached the end without finding it.
return File.ReadLines(fileName).FirstOrDefault(x => x.Contains(text)) is not null;
}
此方法的好处是它在找到值时立即返回 true。 如果找不到文本,它只会读取整个文件。 如果您正在处理通常包含您的搜索的大文件,这可以提高性能。
你可以试试这个代码:
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
for (int x = 0; x < lines.Length - 1; x++)
{
if (lines[x].Contains(domain, StringComparison.InvariantCultureIgnoreCase)
{
sr.Close();
MessageBox.Show("there is a match");
}
}
if (sr != null)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
尝试尝试捕捉:
string x;
string log = @"C:\Users\Log.txt";
string ruta = @"C:\Users\x.txt";
if (File.Exists(ruta))
{
try
{
x = File.ReadAllText(ruta);
}
catch (Exception ex)
{
File.AppendAllText(ruta, "Something");
File.AppendAllText(log, Environment.NewLine + DateTime.Now.ToString() + ": The file not contain a string. " + ex.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.