Click here to Skip to main content
15,559,682 members
Articles / Web Development / ASP.NET
Posted 7 Jun 2012


3 bookmarked

Extended search method

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


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.


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();
        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.


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 9:07
truonghan110-Jun-12 9:07 
GeneralRe: My vote of 5 Pin
AmitGajjar10-Jun-12 18:47
professionalAmitGajjar10-Jun-12 18: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.