Search code examples
c#stringsplitcompareequality

C# - Compare two strings by using dots in the strings and using wildcards: *


I have two string variables that i want to compare.

var compareA = "something.somethingelse.another.something2"
var compareB = "*.another.something2"

I want to compare this, and the result is: True.

var compareC = "something.somethingelse.*"

compared to compareA, the result should also be: True.

Of course, the fact that both variables can contain N dots also complicates the task. How would you start for him?

I was tried this:

 static void Main(string[] args)
    {

        var A = CompareString("*.something", "other.Another.something"); //I need this is true!
        var B = CompareString("something.Value.Other.*", "something.Value.Other.SomethingElse"); //I need this is true
        var C = CompareString("something.Value.Other", "something.Value.Other.OtherElse"); //I need this is False
        var D = CompareString("*.somethingElse", "other.another.Value"); //I Need this is false

        Console.WriteLine("It is need True: {0}", A);
        Console.WriteLine("It is need True: {0}", B);
        Console.WriteLine("It is need False: {0}", C);
        Console.WriteLine("It is need False: {0}", D);
}



        private static bool CompareString(string first, string second) 
    {
        var resume = false;
        var firstSplit = first.Split('.');
        var secondSplit = second.Split('.');
        foreach (var firstItem in firstSplit)
        {
            foreach (var secondItem in secondSplit)
            {
                if (firstItem == "*" || secondItem == "*" || string.Equals(firstItem.ToLower(), secondItem.ToLower()))
                {
                    resume = true;
                }
                else
                {
                    resume = false;
                }
            }
        }
        return resume;
    }

The results are good, but I think it can be done differently, and the reasoning may be wrong.


Solution

  • Assuming the following:

    • Compare 1 string to another and it's the full string of 1 contained inside the longer of the 2 strings.
    • Case to be ignored as well as culture.
    • The full stops are considered part of the string, not actually as a separator.
    • Wildcards can be used to state how 1 string can be contained within another.

    You should be able to use the following

        private bool HasMatch(string textToSearch, string searchText)
        {
            if (textToSearch.Length < searchText.Length) return false;
    
            var wildCardIndex = searchText.IndexOf('*');
    
            if (wildCardIndex == -1)
            {
                return textToSearch.Equals(searchText, StringComparison.InvariantCultureIgnoreCase);
            }
            else
            {
                if (wildCardIndex == 0)
                {
                    var text = searchText.TrimStart('*');
                    return textToSearch.EndsWith(text, StringComparison.InvariantCultureIgnoreCase);
                }
                if (wildCardIndex == (searchText.Length - 1))
                {
                    var text = searchText.TrimEnd('*');
                    return textToSearch.StartsWith(text, StringComparison.InvariantCultureIgnoreCase);
                }                   
            }
    
            return false;
        }