-3

//the word skill it's a substring for two string i want to compare based it

string first = "skill.Name";
string second = "jobskillRelation";
first.Contains(second);
Cid
  • 14,968
  • 4
  • 30
  • 45
  • split, loop, compare. – Cid Dec 29 '19 at 11:51
  • 1
    Does this answer your question https://stackoverflow.com/questions/5848337/how-can-i-check-if-a-string-exists-in-another-string – Shahid Manzoor Bhat Dec 29 '19 at 11:53
  • 1
    Could you clarify the question? What's the output you're trying to get for these two strings? – Mureinik Dec 29 '19 at 12:11
  • If you are looking for [longest common substring](https://en.wikipedia.org/wiki/Longest_common_substring_problem), there's no built in utility to compute it. You can either implement it yourself (see wiki link above), or search for a library. – Tiberiu Maran Dec 29 '19 at 12:15
  • 1
    please reformulate the question, we can't possibly understand it. – Stephane Dec 29 '19 at 12:17

2 Answers2

-1

If you want to compare two string to see if both contain a certain keyword, this may help.

    Boolean compare(string first, string second, string keyword)
    {
        if (first.Contains(keyword) && second.Contains(keyword))
            return true;
        return false;
    }
Mehdi Navran
  • 64
  • 10
-1

You can use Longest Common Substring code provided here, the C# version is like this:

    public static string lcs(string a, string b)
    {
        var lengths = new int[a.Length, b.Length];
        int greatestLength = 0;
        string output = "";
        for (int i = 0; i < a.Length; i++)
        {
            for (int j = 0; j < b.Length; j++)
            {
                if (a[i] == b[j])
                {
                    lengths[i, j] = i == 0 || j == 0 ? 1 : lengths[i - 1, j - 1] + 1;
                    if (lengths[i, j] > greatestLength)
                    {
                        greatestLength = lengths[i, j];
                        output = a.Substring(i - greatestLength + 1, greatestLength);
                    }
                }
                else
                {
                    lengths[i, j] = 0;
                }
            }
        }
        return output;
    }

so the usage will be:

var LCS = lcs(first,second)
roozbeh S
  • 1,084
  • 1
  • 9
  • 16