[英]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.