Search code examples
c#stringlinqasp.net-core

Search a string and check if it is incomplete in array of string


I have a simple array of strings example given below returning from an API, which we do not have a much control.

I am searching for "ABC HG" or "ABCHG". This is incomplete string as it should be either ABC HGL or any other characters after "HG" if you see in the array example. This is an API, which gives me all results even it is a partial string. However, this string example search should be considered as incomplete string, and should not give me any result. How do I do this through linq or elegant way?

string[] strings = {"This is not ABC HGL",
"I am going to ABC HGL",
"What is ABC HGT",
"Excellent ABC HGU"}

I simply need to find for a search string, if it is an incomplete string and do not result anything.

**Please note the input search string can be with or without space but the array results is always with space **


Solution

  • var searchStr = "ABC HG";
    string[] strings =
    {
        "This is not ABC HGL",
        "I am going to ABC HG.",
        "What is ABC Hg",
        "ABC HG is excellent ",
        "I am ABC HGU in this world"
    };
    
    var results = strings.Where(x => x.StartsWith($"{searchStr} ") ||
                                     x.Contains($" {searchStr} ") ||
                                     x.Contains($".{searchStr} ") ||
                                     x.Contains($" {searchStr}.") ||
                                     x.EndsWith($" {searchStr}")).ToList();
    foreach (var result in results)
        Console.WriteLine(result);
    

    Output:

    I am going to ABC HG.
    ABC HG is excellent
    

    Edit: Improved original answer

    Updated the code to include:

    • matching strings between space
    • ignoring special characters such as commas, periods and others

    For example searching forABCHGLto yield a match for -ABC HGL..

    static void Main(string[] args)
    {
        var searchStr = "ABC HG";
        string[] strings =
        {
            "My friend name is Mr.ABC HG.",
            "Where is ABC HGL.",
            "Email is ABC@ABC .HG",
            "Who is ABC HGLM",
            "I am going to (ABC HG)",
            "What is ABC Hg",
            "ABC HG is excellent ",
            ",ABC HG! is not coming",
            "I am ABC HGU in this world"
        };
    
        var results = strings.Where(x => IsMatched(x, searchStr) ||
                                         MatchBtwSpace(x, searchStr)).ToList();
        foreach (var result in results)
            Console.WriteLine(result);
    }
    
    private static bool IsMatched(string line, string query)
    {
        if (line.Contains(query))
        {
            // inject whitespace at start and end of line to reduce complexity
            line = $" {line} ";
            int queryStartIndex = line.IndexOf(query);
            int queryEndIndex = queryStartIndex + query.Length - 1;
    
            // check if between query contains letters or numbers
            if (char.IsLetterOrDigit(line[queryStartIndex - 1]) ||
                char.IsLetterOrDigit(line[queryEndIndex + 1]))
                return false;
    
            return true;
        }
        return false;
    }
    
    private static bool MatchBtwSpace(string str, string query)
    {
        // split line to words
        var words = str.Split(" ");
        for (int i = 0; i < words.Length; i++)
        {
            if (i + 1 == words.Length)
                break;
            var match = IsMatched($"{words[i]}{words[i + 1]}", query);
            if (match)
                return true;
        }
    
        return false;
    }
    

    Output:

    
    searchStr = "ABC HG" OR "ABCHG"
    //My friend name is Mr.ABC HG.
    //I am going to (ABC HG)
    //ABC HG is excellent
    //,ABC HG! is not coming
    
    searchStr = "[email protected]"
    //Email is ABC@ABC .HG