tags:

views:

95

answers:

4

I'm looking for a simple way to discern if a string contains any part of another string (be that regex, built in function I don't know about, etc...). For Example:

string a = "unicorn";
string b = "cornholio";
string c = "ornament";
string d = "elephant";

if (a <comparison> b)
{
    // match found ("corn" from 'unicorn' matched "corn" from 'cornholio')
}

if (a <comparison> c)
{
    // match found ("orn" from 'unicorn' matched "orn" from 'ornament')
}

if (a <comparison> d)
{
    // this will not match
}

something like if (a.ContainsAnyPartOf(b)) would be too much to hope for.

Also, I only have access to .NET 2.0.

Thanks in advance!

+5  A: 

This method should work. You'll want to specify a minimum length for the "part" that might match. I'd assume you'd want to look for something of at least 2, but with this you can set it as high or low as you want. Note: error checking not included.

public static bool ContainsPartOf(string s1, string s2, int minsize)
{
    for (int i = 0; i <= s2.Length - minsize; i++)
    {
        if (s1.Contains(s2.Substring(i, minsize)))
            return true;
    }
    return false;
}
juharr
Perfect. I was hoping there would be something in .NET for it but this works just as well!
Aaron
A: 

Your requirements are a little vague.

You need to define a minimum length for the match...but implementing an algorithm shouldn't be too difficult when you figure that part out.

I'd suggest breaking down the string into character arrays and then using tail recursion to find matches for the parts.

Justin Niessner
+1  A: 

Your best bet, according to my understanding of the question, is to compute the Levenshtein (or related values) distance and compare that against a threshold.

Benjamin Podszun
+3  A: 

I think you're looking for this implementation of longest common substring?

great_llama