string s1="8405";
string s2="8425";if(s1.ToList()..Intersect(s2ToList().).ToList().Count()>=3)//交集
{
//相似
}
else
{
//不相似
}
string s2="8425";if(s1.ToList()..Intersect(s2ToList().).ToList().Count()>=3)//交集
{
//相似
}
else
{
//不相似
}
string s1 = this.textBox1.Text.Trim();
string s2 = this.textBox2.Text.Trim();
int count = 0;
foreach (char c1 in s1.ToArray())
{
foreach (char c2 in s2.ToArray())
{
if (c1 == c2)
{
count++;
break;
}
}
}
if (count >= 3)
{
Console.WriteLine("相似");
}
else
{
Console.WriteLine("不相似");
}
7334,3775,按照你的方法为true,实际情况为false,也不对
然后分别循环,截取子串,3个字符,对比,判断contains
string s1 = "8990";
string s2 = "9989";
char[] arry1 = s1.ToArray().OrderBy(x => x).ToArray(); //排序
char[] arry2 = s2.ToArray().OrderBy(x => x).ToArray(); //排序
int count = 0;
for (int i = 0; i < arry1.Length; i++)
{
for (int j = 0; j < arry2.Length; j++)
{
if (arry1[i] == arry2[j]) //有一个相等,判断后面2个是否也相等,相等则认为相似
{
count = 1;
bool flag = false;
for (int x = 0; x < 3; x++) //判断后面2个是否也相等
{
if (i + x + 1 >= arry1.Length || j + x + 1 >= arry2.Length)
break; if (arry1[i + x + 1] != arry2[j + x + 1]) //不相等,则此连续段不相似
{
flag = true;
break;
}
count++; }
if (flag) //此连续段不相似,重新往后判断
break;
if (count >= 3)
{
MessageBox.Show("相似");
return;
}
}
}
}
if (count < 3)
{
MessageBox.Show("不相似");
}
string s2 = "2345";
bool 相似 = s1.Join(s2, x => x, x => x, (x, y) => x == y).Count(x => x) > 3;
7334,3775,按照你的方法为true,实际情况为false,也不对
他按照你的要求 他写的就是对的。。如果不对请把 c1 == c2改成值比较c1.Equals(c2)
public class StringMatch
{
public static int Match(string s1,string s2)
{
//按最短的字符串查找
if (s1.Length>s2.Length)
{
string stmp = s1;
s1 = s2;
s2 = s1;
} int iMatch = 0;
for (int i = 0; i < s1.Length; i++)
{
int c1 = GetCharCount(s1, s1.Substring(i, 1),i);
int c2 = GetCharCount(s2, s1.Substring(i, 1));
if (c1<=c2)
{
iMatch++;
}
}
return iMatch;
}
public static int GetCharCount(string str, string sChar)
{
int pos = 0;
int Count = 0;
while (pos != -1)
{
pos = str.IndexOf(sChar, pos);//获取字符的索引
if (pos != -1)
{
Count++;
pos++; }
}
return Count; }
public static int GetCharCount(string str,string sChar,int endPos)
{
int pos = 0;
int Count = 0;
while (pos != -1)
{
pos = str.IndexOf(sChar, pos);//获取字符的索引
if (pos != -1)
{
if (pos>endPos)
{
return Count;
}
else
{
Count++;
pos++;
}
}
}
return Count; }
}
测试结果: int m= StringMatch.Match("1234", "1235");
楼主再举例再不行,我也没招了
string s1 = "8998"; //"8998","7334","1245"
string s2 = "8908"; //"8908","3775","1345" var ex = s1.ToArray().Intersect(s2.ToArray()).ToArray(); //交集
int count = 0;
foreach (char c in ex) //看交集在s1和s2中出现的次数
{
int x1 = 0, x2 = 0;
foreach (char c1 in s1.ToArray())
if (c == c1)
x1++;
foreach (char c2 in s2.ToArray())
if (c == c2)
x2++; count += Math.Min(x1, x2); //取最小的次数
}
if (count >= 3)
{
MessageBox.Show("相似");
}
else
{
MessageBox.Show("不相似哦");
}
/// <summary>
/// 验证字符StrB和StrA是否相似
/// </summary>
/// <param name="StrA"></param>
/// <param name="StrB"></param>
/// <returns></returns>
public bool IsLike(string StrA, string StrB)
{
List<string> list = new List<string>();
int count = 0;
for (int i = 0; i < StrA.Length; i++)
{
string str = StrA.Substring(i, 1);
list.Add(str);
}
for (int i = 0; i < StrB.Length; i++)
{
string str = StrB.Substring(i, 1);
if (list.Contains(str))
count++;
}
if (count >= 3)
return true;
else
return false;
}
string aa1 = "9898", aa2 = "8908", aa3 = "";
for (int a = 0; a < aa1.Length - 1; a++)
if (aa2.Contains(aa1.Substring(a, 1)))
aa3 += aa1.Substring(a, 1);http://blog.csdn.net/xianfajushi/article/details/11741799
var bb = aa[0].Select(cc => aa[1].Contains(cc.ToString())).Count(ee => ee == true);
var dd = aa[1].Select(cc => aa[0].Contains(cc.ToString())).Count(ee => ee == true);
if(bb>=3&&dd>=3)...
var hh = aa[0].Where(gg => aa[1].Contains(gg)).Count();
static void Main(string[] args)
{
string str1 = "8990";
string str2 = "99089";
Hashtable ht1 = getHt(str1);
Hashtable ht2 = getHt(str2); int iCount = 0;
foreach (char item1 in ht1.Keys)
{
foreach (char item2 in ht2.Keys)
{
if (item1 == item2)
{
iCount++;
break;
}
}
} if (iCount > 2)
{ Console.WriteLine("相似");
}
else
{
Console.WriteLine("不相似");
}
Console.ReadKey();
} public static Hashtable getHt(string s)
{
Hashtable ht = new Hashtable();
foreach (char item in s)
{
if (ht.Contains(item))
{
int icount = (int)ht[item];
icount++;
ht[item] = icount; }
else
{
ht.Add(item, 1);
}
} return
ht;
}