Click here to Skip to main content
15,879,535 members
Articles / Web Development / ASP.NET
Tip/Trick

Extended search method

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
7 Jun 2012CPOL1 min read 15.5K   252   3   2
An efficient extended search API to search from a text file.

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.

C#
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.

C#
ExtendedSearch.TextFileSearch newSearch = new ExtendedSearch.TextFileSearch();
var result = newSearch.SearchString("TextFile1.txt", "device");

Hope this library will help you in your project somewhere.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
truonghan110-Jun-12 8:07
truonghan110-Jun-12 8:07 
GeneralRe: My vote of 5 Pin
AmitGajjar10-Jun-12 17:47
professionalAmitGajjar10-Jun-12 17:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.