for (int i = 0; i < arrStr1.length; i++)
{
for (int j = 0; j < arrStr2.length; j++)
{
if (arrStr1[i] == arrStr2[j + cnt])
{
str += arrStr1[i].ToString();
cnt++;
break;
}
else{
continue;
}
}
}
public static string Show(string CompareStr1, string CompareStr2)
{
//将两个字符串拆分成字符数组 使用ToCharArray方法
var arrStr1 = CompareStr1.ToCharArray();
var arrStr2 = CompareStr2.ToCharArray();
List<CompareResult> list = new List<CompareResult>();
var str = "";
var cnt = 0;
for (int i = 0; i < arrStr1.Length; i++)
{
str = "";
cnt = 0;
if (!arrStr2.Any(o => o == arrStr1[i]))
{
continue;
}
for (int j = 0; j < arrStr2.Length; j++)
{
if (i + cnt >= arrStr1.Length)
{
i = arrStr1.Length;
break;
}
else if (arrStr1[i + cnt] == arrStr2[j])
{
str += arrStr1[i + cnt].ToString();
cnt++;
}
else
{
if (cnt > 0)
{
break;
}
}
}
list.Add( new CompareResult { KeyStr = str, KeyCount = cnt });
}
var result = list.OrderByDescending(o=>o.KeyCount).FirstOrDefault();
return result.KeyStr;
}
//定义一个类,用来存放紧邻字符的相同字符串以及其字符个数
public class CompareResult
{
public string KeyStr{ get; set; }
public int KeyCount { get; set; }
}
原文:https://www.cnblogs.com/wangxiaojun1/p/11406224.html