[英]How I can check if substring contain another substring c#
//技能這個詞是我想比較的兩個字符串的子字符串
string first = "skill.Name";
string second = "jobskillRelation";
first.Contains(second);
如果您想比較兩個字符串以查看它們是否都包含某個關鍵字,這可能會有所幫助。
Boolean compare(string first, string second, string keyword)
{
if (first.Contains(keyword) && second.Contains(keyword))
return true;
return false;
}
你可以使用這里提供的最長公共子串代碼,C#版本是這樣的:
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;
}
所以用法是:
var LCS = lcs(first,second)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.