Introduction
One day my friend asked me for a solution. He is developing a searching method to search from a text file. I created an API class library that can be helpful to people with similar searching requirements.
Problem Statement
Search the string within the text file. The search result should be a line from the text file.
If we are passing a string for search, spelling mistakes should also be
caught up to a certain level.
Example: If the user is searching for "device" then our result should be like below:
Source Text:
Hello this is device
hello this is edvice
Hello this is edevice
Hello I have vicede
Hellow I have ievced
Result Text :
Hello this is device
hello this is edvice
Hello I have vicede
Hellow I have ievced
So conclusion is the string with the same length and same characters should be fetched from the given text file.
Solution
I have created an API class library that can do our job. I have created a class with a method named SearchString
.
Below code snippet is what I have done.
public class TextFileSearch
{
private IEnumerable<string> searchResult;
public IEnumerable<string> SearchString(string filePath, string searchString)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException("Given filePath file does not exist.");
}
if (string.IsNullOrEmpty(filePath) || string.IsNullOrWhiteSpace(searchString))
{
throw new ArgumentNullException(
"Given filePath or searchString should not be null or empty.");
}
searchString = searchSimilarString(searchString);
var lines = File.ReadAllLines(filePath);
searchResult = lines.Where(s => s.Split(' ').Any(f => (
f.Length == searchString.Length && searchSimilarString(f) == searchString)));
return searchResult;
}
private string searchSimilarString(string searchString)
{
char[] sortedArray = searchString.ToArray();
Array.Sort(sortedArray);
return string.Join<char>("", sortedArray);
}
}
The searchSimilarString
function will convert the whole string into
a character array and sort it. After sorting it will join the character
so our search string "device" and "edvice" will be similar.
You can also see that we have added f.Length== searchString.Length
in the LINQ query to improve performance. We are not converting the string array if
the search string length is more or less than the character length.
How to Use
Add a reference of the ExtendedSearch library and use the below code to do your search in
a text file.
ExtendedSearch.TextFileSearch newSearch = new ExtendedSearch.TextFileSearch();
var result = newSearch.SearchString("TextFile1.txt", "device");
Hope this library will help you in your project somewhere.