# 比较两个字符串的最长的相同字符串 及其位置和百分比

  /// <summary>
///
/// </summary>
/// <param name="DataKey"></param>
/// <param name="DataSoure"></param>
/// <param name="minPercentage"></param>
/// <param name="maxPercentage"></param>
/// <param name="minstarLoca"></param>
/// <param name="maxstarLoca"></param>
/// <returns></returns>
public static string getSomeString(string DataKey
,string DataSoure
,out string minPercentage
,out string maxPercentage
,out int minstarLoca
,out int maxstarLoca)
{
string strReturn = "";
int strBeginStar = 0,strlength=1,maxlength=0;
bool IsGoOn=true;
string strTemp ="";
//使用短的区匹配长的 加快速度
string strKey, strSoure;
strKey = DataKey.Length >= DataSoure.Length ? DataSoure : DataKey;
strSoure = DataKey.Length > DataSoure.Length ? DataKey : DataSoure;
minstarLoca = 0; maxstarLoca = 0; minPercentage = "0"; maxPercentage = "0";
while (maxlength <= strKey.Length)
{
strTemp = strKey.Substring(strBeginStar, strlength);
while (strSoure.IndexOf(strTemp) > -1 && maxlength <= strKey.Length)
{
minstarLoca = strBeginStar;
maxstarLoca = strSoure.IndexOf(strTemp);
strReturn = strTemp;
strlength = strlength + 1;
maxlength = strlength + strBeginStar;
if (maxlength <= strKey.Length)
{
strTemp = strKey.Substring(strBeginStar, strlength);
}
}
strBeginStar = strBeginStar + 1;
maxlength = strlength + strBeginStar;
}
minPercentage = ((double)strReturn.Length * 100 / strSoure.Length).ToString();
maxPercentage = ((double)strReturn.Length * 100 / strKey.Length).ToString();
return strReturn;
}

        protected void Button1_Click(object sender, EventArgs e)
{
string strMax,strMin;
int minstar,maxstar;
string str = getSomeString(TextBox1.Text.Trim(), TextBox2.Text.Trim(), out strMin, out strMax, out minstar, out maxstar);
Label1.Text = "最大百分比为：" + strMax + "最小百分比为：" + strMin + "在小字符串中开始位置：" + minstar.ToString() + "在大字符串中开始位置：" + maxstar.ToString()+ "<br/>匹配字符串为：" + str;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：比较两个字符串的最长的相同字符串 及其位置和百分比 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)