Click here to Skip to main content
11,720,731 members (54,899 online)
Click here to Skip to main content

Extended search method

, 7 Jun 2012 CPOL 4.2K 151 3
Rate this:
Please Sign up or sign in to vote.
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.

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.

License

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

Share

About the Author

@AmitGajjar
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
truonghan110-Jun-12 8:07
membertruonghan110-Jun-12 8:07 
GeneralRe: My vote of 5 Pin
amitgajjar10-Jun-12 17:47
memberamitgajjar10-Jun-12 17:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by @AmitGajjar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid