#2
miaoge9110152021-07-13 10:55
private void button1_Click(object sender, EventArgs e)
{ //# 求2个字符串的最大相似度【字符长度10以内】 string s1 = textBox1.Text.Trim(); string s2 = textBox2.Text.Trim(); if (s1.Length < 10) { s1 = s1 + "①②③④⑤⑥⑦⑧⑨⑩"; s1 = s1.Substring(0,10); } if (s2.Length < 10) { s2 = s2 + "⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳"; s2 = s2.Substring(0,10); } textBox1.Text = s1; textBox2.Text = s2; StringBuilder sb = new StringBuilder(); sb.Append("字符串1----------------------------------------\r\n"); List<string> List1 = new List<string>();//第一个集合 List<string> List2 = new List<string>();//第二个集合 for (int i = 1; i <= 10; i++) { sb.Append(s1.Substring(i - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1)); for (int i2 = 2; i2 <= 10; i2++) { if (i < i2) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1)); for (int i3 = 3; i3 <= 10; i3++) { if (i2 < i3) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1)); for (int i4 = 4; i4 <= 10; i4++) { if (i3 < i4) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1)); for (int i5 = 5; i5 <= 10; i5++) { if (i4 < i5) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1)); for (int i6 = 6; i6 <= 10; i6++) { if (i5 < i6) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1)); for (int i7 = 7; i7 <= 10; i7++) { if (i6 < i7) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1)); for (int i8 = 8; i8 <= 10; i8++) { if (i7 < i8) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + s1.Substring(i8 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + s1.Substring(i8 - 1, 1)); for (int i9 = 9; i9 <= 10; i9++) { if (i8 < i9) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + s1.Substring(i8 - 1, 1) + s1.Substring(i9 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + s1.Substring(i8 - 1, 1) + s1.Substring(i9 - 1, 1)); for (int i10 = 10; i10 <= 10; i10++) { if (i9 < i10) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + s1.Substring(i8 - 1, 1) + s1.Substring(i9 - 1, 1) + s1.Substring(i10 - 1, 1) + ","); List1.Add(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + s1.Substring(i5 - 1, 1) + s1.Substring(i6 - 1, 1) + s1.Substring(i7 - 1, 1) + s1.Substring(i8 - 1, 1) + s1.Substring(i9 - 1, 1) + s1.Substring(i10 - 1, 1)); } } } } } } } } } } } } } } } } } } } sb.Append("\r\n字符串2----------------------------------------\r\n"); for (int i = 1; i <= 10; i++) { sb.Append(s2.Substring(i - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1)); for (int i2 = 2; i2 <= 10; i2++) { if (i < i2) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) ); for (int i3 = 3; i3 <= 10; i3++) { if (i2 < i3) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1)); for (int i4 = 4; i4 <= 10; i4++) { if (i3 < i4) { sb.Append(s1.Substring(i - 1, 1) + s1.Substring(i2 - 1, 1) + s1.Substring(i3 - 1, 1) + s1.Substring(i4 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1)); for (int i5 = 5; i5 <= 10; i5++) { if (i4 < i5) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) ); for (int i6 = 6; i6 <= 10; i6++) { if (i5 < i6) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) ); for (int i7 = 7; i7 <= 10; i7++) { if (i6 < i7) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1)); for (int i8 = 8; i8 <= 10; i8++) { if (i7 < i8) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + s2.Substring(i8 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + s2.Substring(i8 - 1, 1) ); for (int i9 = 9; i9 <= 10; i9++) { if (i8 < i9) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + s2.Substring(i8 - 1, 1) + s2.Substring(i9 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + s2.Substring(i8 - 1, 1) + s2.Substring(i9 - 1, 1) ); for (int i10 = 10; i10 <= 10; i10++) { if (i9 < i10) { sb.Append(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + s2.Substring(i8 - 1, 1) + s2.Substring(i9 - 1, 1) + s2.Substring(i10 - 1, 1) + ","); List2.Add(s2.Substring(i - 1, 1) + s2.Substring(i2 - 1, 1) + s2.Substring(i3 - 1, 1) + s2.Substring(i4 - 1, 1) + s2.Substring(i5 - 1, 1) + s2.Substring(i6 - 1, 1) + s2.Substring(i7 - 1, 1) + s2.Substring(i8 - 1, 1) + s2.Substring(i9 - 1, 1) + s2.Substring(i10 - 1, 1)); } } } } } } } } } } } } } } } } } } } sb.Append("\r\n----------------------------------------"); textBox3.Text = sb.ToString(); bool maxbo = false; for (int i = 10; i > 0; i--) { if (maxbo == false) { var ww1 = List1.Where(w => w.Length == i).GroupBy(m => new { m }). Select(group => new { gro = group.Key }).ToList(); var ww2 = List2.Where(w => w.Length == i).GroupBy(m => new { m }). Select(group => new { gro = group.Key }).ToList(); foreach (var item in ww1) { foreach (var item2 in ww2) { if (item.gro.m == item2.gro.m) { MessageBox.Show("匹配到的第一个最大相似数是:" + item.gro.m); maxbo = true; break; } } } } } } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = "Abcd"; textBox2.Text = "A1Vcd"; } |
A1='Abcd'
A2='A1Vcd'
需要的结果是'Acd',具体问题是来自于表内字段,请问各位大佬,有无函数可以直接解决,谢谢!