mo alaz
mo alaz

Reputation: 4749

Comparing two strings with different orders

I have a dictionary with a list of strings that each look something like:

"beginning|middle|middle2|end"

Now what I wanted was to do this:

List<string> stringsWithPipes = new List<string>();
stringWithPipes.Add("beginning|middle|middle2|end");
...

if(stringWithPipes.Contains("beginning|middle|middle2|end")
{
 return true;
}

problem is, the string i'm comparing it against is built slightly different so it ends up being more like:

if(stringWithPipes.Contains(beginning|middle2|middle||end)
{
 return true;
}

and obviously this ends up being false. However, I want to consider it true, since its only the order that is different. What can I do?

Upvotes: 2

Views: 208

Answers (4)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

Try this instead::

if(stringWithPipes.Any(P => P.split('|')
                             .All(K => "beginning|middle2|middle|end".split('|')
                             .contains(K)))

Hope this will help !!

Upvotes: 1

Habib
Habib

Reputation: 223247

You can split your string on | and then split the string to be compared, and then use Enumerable.Except along with Enumerable.Any like

List<string> stringsWithPipes = new List<string>();
stringsWithPipes.Add("beginning|middle|middle2|end");
stringsWithPipes.Add("beginning|middle|middle3|end");
stringsWithPipes.Add("beginning|middle2|middle|end");

var array = stringsWithPipes.Select(r => r.Split('|')).ToArray();

string str = "beginning|middle2|middle|end";
var compareArray = str.Split('|');

foreach (var subArray in array)
{
    if (!subArray.Except(compareArray).Any())
    {
        //Exists
        Console.WriteLine("Item exists");
        break;
    }
}

This can surely be optimized, but the above is one way to do it.

Upvotes: 2

Bob Vale
Bob Vale

Reputation: 18474

You need to split on a delimeter:

 var searchString = "beginning|middle|middle2|end";
 var searchList = searchString.Split('|');

 var stringsWithPipes = new List<string>();
 stringsWithPipes.Add("beginning|middle|middle2|end");
 ...

 return stringsWithPipes.Select(x => x.Split('|')).Any(x => Match(searchList,x));

Then you can implement match in multiple ways

First up must contain all the search phrases but could include others.

 bool Match(string[] search, string[] match) {
    return search.All(x => match.Contains(x));
 }

Or must be all the search phrases cannot include others.

 bool Match(string[] search, string[] match) {
    return search.All(x => match.Contains(x)) && search.Length == match.Length;
 }

Upvotes: 0

Pierre-Luc Pineault
Pierre-Luc Pineault

Reputation: 9191

That should work.

List<string> stringsWithPipes = new List<string>();
stringsWithPipes.Add("beginning|middle|middle2|end");

string[] stringToVerifyWith = "beginning|middle2|middle||end".Split(new[] { '|' }, 
                                                                 StringSplitOptions.RemoveEmptyEntries);
if (stringsWithPipes.Any(s => !s.Split('|').Except(stringToVerifyWith).Any()))
{
    return true;
}

The Split will remove any empty entries created by the doubles |. You then check what's left if you remove every common element with the Except method. If there's nothing left (the ! [...] .Any(), .Count() == 0 would be valid too), they both contain the same elements.

Upvotes: 0

Related Questions