标题:找出两个字符串中的不同部分
只看楼主
fifowl2001
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2020-9-12
结帖率:60%
 问题点数:0 回复次数:2 
找出两个字符串中的不同部分
A1='Abcd'
A2='A1Vcd'

需要的结果是'Acd',具体问题是来自于表内字段,请问各位大佬,有无函数可以直接解决,谢谢!
搜索更多相关主题的帖子: 字符串 字段 函数 不同 结果 
2021-06-09 11:56
miaoge911015
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-9-23
得分:0 
   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";
        }
2021-07-13 10:55
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:190
帖 子:3125
专家分:8340
注 册:2015-3-25
得分:0 
以下是引用fifowl2001在2021-6-9 11:56:55的发言:

A1='Abcd'
A2='A1Vcd'

需要的结果是'Acd',具体问题是来自于表内字段,请问各位大佬,有无函数可以直接解决,谢谢!

楼主,如果你A2是
A2='A1Vced'
那你要的结果是什么
2021-07-13 12:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505978-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055240 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved