Last active
December 2, 2020 10:23
-
-
Save lukaabra/1e6e7d1fca58e9abae9b2c4b3471fe9c to your computer and use it in GitHub Desktop.
Finds a specified keyword inside of a text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Returns true or false depending on if a text file contains the specified word in the content | |
/// Example: | |
/// FindWordInFile("error", "C:/user/Desktop/test.txt"); | |
/// </summary> | |
/// <param name="word">Word to find in text file</param> | |
/// <param name="filePath">Absolute path to the text file</param> | |
/// <returns>True if the word is inside of the file, false if not.</returns> | |
static public bool FindWordInFile(string word, string filePath) | |
{ | |
bool contains = false; | |
// Read each line of the file into a string array | |
string[] lines = System.IO.File.ReadAllLines(filePath); | |
foreach (string line in lines) | |
{ | |
// Check if word is in the read line | |
contains = line.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0; | |
if (contains) | |
return contains; | |
} | |
// No matches found | |
return contains; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment