如何在大型文本文档中查找具有唯一字符串内容的特定行
How to find specific line with unique string content in large text document
我正在尝试检查大约 500 000 行的大型文本文档是否包含特定行,问题是如果我这样找到它:
string searchLine = "line 4";
using (StreamReader sr = new StreamReader(filePath))
{
string contents = sr.ReadToEnd();
if (contents.Contains(searchLine))
{
Console.WriteLine("line exist");
}
else
{
Console.WriteLine("line does not exist");
}
}
文档内容是,我不接受重复写它,所有字符串都是唯一的:
line 1
line 2
line 3
line 4
line 5
line 47
所以我得到的答案是 "line exist" for "line 4" 是对的,但是如果我从订单中删除它,并再次检查文件中是否有相同的字符串 "line 4",它说"line exist",因为它似乎在文本文件内容中找到了所有 4 个数字,只有当我删除 "line47",然后 "line does not exist"。
所以我想知道如何在大型文本文档中找到具有唯一字符串内容的特定行。
您可以使用以下代码来搜索确切的内容。
public string ExactReplace(string input, string find, string replace)
{
string textToFind = string.Format(@"\b{0}\b", find);
return Regex.Replace(input, textToFind, replace);
}
然后你可以这样称呼它
string fulltext = sr.ReadToEnd();
string result = text.ExactReplace(fulltext, "line 4", "");
元字符 \b
是一个类似于插入符号和美元符号的锚点。它在称为 "word boundary" 的位置匹配。此匹配为零长度。
有三个不同的位置可以作为单词边界:
- 在字符串的第一个字符之前,如果第一个字符是a
词字符。
- 在字符串的最后一个字符之后,如果最后一个字符是单词字符。
- 字符串中两个字符之间,一个是单词字符,另一个不是单词字符。
有关 Word Boundaries 的更多信息
sr.ReadToEnd();
不是逐行读取文件,而是读取从当前位置到流末尾的所有字符。
虽然 Readline()
方法从当前流中读取一行字符并且 returns 数据作为字符串
Readline()
方法将逐行读取文件,如下所示:
string currentLine;
bool exist = false;
using (StreamReader sr = new StreamReader(filepath))
{
while ((currentLine = sr.ReadLine()) != null)
{
if (currentLine == "line 4")
exist = true;
}
}
Console.WriteLine(exist ? "line exist" : "line does not exist");
或者您也可以比较:
string.Equals(currentLine, "line 4")
而不是
currentLine == "line 4"
我正在尝试检查大约 500 000 行的大型文本文档是否包含特定行,问题是如果我这样找到它:
string searchLine = "line 4";
using (StreamReader sr = new StreamReader(filePath))
{
string contents = sr.ReadToEnd();
if (contents.Contains(searchLine))
{
Console.WriteLine("line exist");
}
else
{
Console.WriteLine("line does not exist");
}
}
文档内容是,我不接受重复写它,所有字符串都是唯一的:
line 1
line 2
line 3
line 4
line 5
line 47
所以我得到的答案是 "line exist" for "line 4" 是对的,但是如果我从订单中删除它,并再次检查文件中是否有相同的字符串 "line 4",它说"line exist",因为它似乎在文本文件内容中找到了所有 4 个数字,只有当我删除 "line47",然后 "line does not exist"。
所以我想知道如何在大型文本文档中找到具有唯一字符串内容的特定行。
您可以使用以下代码来搜索确切的内容。
public string ExactReplace(string input, string find, string replace)
{
string textToFind = string.Format(@"\b{0}\b", find);
return Regex.Replace(input, textToFind, replace);
}
然后你可以这样称呼它
string fulltext = sr.ReadToEnd();
string result = text.ExactReplace(fulltext, "line 4", "");
元字符 \b
是一个类似于插入符号和美元符号的锚点。它在称为 "word boundary" 的位置匹配。此匹配为零长度。
有三个不同的位置可以作为单词边界:
- 在字符串的第一个字符之前,如果第一个字符是a 词字符。
- 在字符串的最后一个字符之后,如果最后一个字符是单词字符。
- 字符串中两个字符之间,一个是单词字符,另一个不是单词字符。
有关 Word Boundaries 的更多信息
sr.ReadToEnd();
不是逐行读取文件,而是读取从当前位置到流末尾的所有字符。
虽然 Readline()
方法从当前流中读取一行字符并且 returns 数据作为字符串
Readline()
方法将逐行读取文件,如下所示:
string currentLine;
bool exist = false;
using (StreamReader sr = new StreamReader(filepath))
{
while ((currentLine = sr.ReadLine()) != null)
{
if (currentLine == "line 4")
exist = true;
}
}
Console.WriteLine(exist ? "line exist" : "line does not exist");
或者您也可以比较:
string.Equals(currentLine, "line 4")
而不是
currentLine == "line 4"