Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compare two strings for matching words

Tags:

string

c#

I have two strings containing letters and numbers separated by spaces. ex "elza7ma wa2fa fel matab" and "2ana ba7eb el za7ma 2awy 2awy"

What is the fastest way to compare this two string to find out whether or not they have a word in common?

I tried to split one of them using string.split and use string.compare on the whole array of words. but this is very slow since I will be comparing a lot of strings.

like image 462
Marwan Avatar asked Sep 24 '10 07:09

Marwan


2 Answers

A LINQ solution

"elza7ma wa2fa fel matab".Split()
                         .Intersect("2ana ba7eb el za7ma 2awy 2awy".Split())
                         .Any();

// as a string extension method
public static class StringExtensions
{
    public static bool OneWordMatches(this string theString, string otherString)
    {
        return theString.Split().Intersect(otherString.Split()).Any();
    }
}

// returns true
"elza7ma wa2fa fel matab 2ana".OneWordMatches("2ana ba7eb el za7ma 2awy 2awy");
like image 126
Russ Cam Avatar answered Sep 24 '22 03:09

Russ Cam


I think the easiest way is to break up the strings into words and use a set structure like HashSet<string> to check for duplicates. For example

public bool HasMatchingWord(string left, string right) { 
  var hashSet = new HashSet<string>(
    left.Split(" ", StringSplitOptions.RemoveEmptyEntries)); 
  return right
    .Split(" ", StringSplitOptions.RemoveEmptyEntries)
    .Any(x => hashSet.Contains(x));
}
like image 20
JaredPar Avatar answered Sep 20 '22 03:09

JaredPar