您需要標記行並使用HashSet。這是最快的方法。把所有詞語的HashSet的再檢查,如果每個字是INIT:
static void Main()
{
var file = @"C:\application\thefile";
var hashSet = new HashSet<string>(new[] { "banana", "horse", "window", "whatever" }.Select(x => x.ToLower()));
foreach (var word in GetWords(file))
{
Console.WriteLine(word);
if (hashSet.Contains(word))
{
//DoSomething();
Console.WriteLine("\tFound!!");
//Continue or Break;
}
}
}
private static IEnumerable<string> GetWords(string file)
{
var rg = new Regex(@"[^\p{L}]");
const int bufferLen = 512;
using (var reader = File.OpenText(file))
{
var word = new StringBuilder();
while (!reader.EndOfStream)
{
var buffer = new char[bufferLen];
var readChars = reader.ReadBlock(buffer, 0, bufferLen);
for (int i = 0; i < readChars; i++)
{
if (rg.IsMatch(buffer[i].ToString()))//end of the word
{
if (word.Length > 0)
{
yield return word.ToString();
word = new StringBuilder();
}
}
else
word.Append(Char.ToLowerInvariant(buffer[i]));
}
}
if (word.Length > 0)
yield return word.ToString();
}
}
,並在這裏VB
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim filename = "C:\application\thefile"
Dim words() As String = {"banana", "horse", "window", "whatever"}
Dim bagOfWords = New HashSet(Of String)(words.Select(Function(x) x.ToLower()))
For Each word As String In GetWords(filename)
Console.WriteLine(word)
If bagOfWords.Contains(word) Then
'DoSomething();
Console.WriteLine(vbTab & "Found!!")
'Exit For if you need to terminate here;
End If
Next
End Sub
Private Iterator Function GetWords(filename As String) As IEnumerable(Of String)
Dim rg = New Regex("[^\p{L}]")
Const bufferLen As Integer = 512
Using reader As New StreamReader(filename)
Dim word = New StringBuilder()
While Not reader.EndOfStream
Dim buffer = New Char(bufferLen - 1) {}
Dim readChars = reader.ReadBlock(buffer, 0, bufferLen)
For i As Integer = 0 To readChars - 1
If rg.IsMatch(buffer(i).ToString()) Then
'end of the word
If word.Length > 0 Then
Yield word.ToString()
word = New StringBuilder()
End If
Else
word.Append([Char].ToLowerInvariant(buffer(i)))
End If
Next
End While
If word.Length > 0 Then
Yield word.ToString()
End If
End Using
End Function
End Module
難道你不想在每個條件之前?你只有第一個。 –
你能否澄清你的問題?我不明白你的意思是「某個文件中的某些單詞*然後使用一個if語句」 - 你想找到單詞然後做別的事嗎?或者你(只)想看看某個單詞是否在某個單詞集中? – Default
當然。更新我的問題。如果在文件中找到單詞(只是一個單詞就足夠了),我確實想做其他的事情。如果他們沒有找到,什麼都不要做。 – MadsTheMan