minAngell 2022-03-25 16:43 采纳率: 100%
浏览 76
已结题

C#字符串对比,并且取出其中不同的字符串

如题:
string str1="123456";
string str2="123abc45ef6ghi";
如何得到str2的abc,ef,ghi.

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-03-25 16:58
    关注

    大概写了个js,C#题主可以按照js代码形式改下,逻辑差不多

    img

    <style>
        .editor {
            border: solid 1px #ccc;
            width: 200px;
            height: 100px;
            margin-right: 10px;
            padding: 10px;
            display: inline-block;
            vertical-align: top;float:left
        }
    </style>
    <div class="editor" contenteditable="true"></div>
    <div class="editor" contenteditable="true"></div>
    
    <input type="button" value="找不同" onclick="findDiff()" />
    <script>
        function getDiff(str1, str2) {
            var diffs = [], s = str1.charAt(0), t = s, index1, index2, diff
            index1 = str2.indexOf(s);
            if (index1 == -1) return [{ diff: str2}];
            for (var i = 1; i < str1.length; i++) {
                var c = str1.charAt(i)
                t += c;
                index2 = str2.indexOf(t);
                if (index1 == index2) { s += c; }
                else {
                    if (index1 == 0) {
                        diffs.push({ diff: s, match: 1 });
                        index2 = str2.indexOf(c);
                        if (index2 != -1) {
                            diff = str2.substring(s.length, index2)
                            diffs.push({ diff});
    
                        }
                        else {
                            diff = str2.substring(s.length);
                            diffs.push({ diff});
                            break;
                        }
                        str2 = str2.substring(s.length + diff.length);
                        index1 = index2 = str2.indexOf(c)
                        t = s = c;
                    }
                    else {
                        diff = str2.substring(0, index1);
                        diffs.push({ diff });
                        diffs.push({ diff: s, match: 1 });
    
                        str2 = str2.substring(index1 + s.length)
    
                        t = c;
                        index2 = str2.indexOf(t);
                        if (index2 == -1) {//剩余字符不包含代查找字符
                            diff = str2;
                            diffs.push({ diff });
                            break;
                        }
                        index1 = index2;
                        s = t;
                    }
                }
            }
            diff = str2.substring(s.length)
            if (diff) {
                diffs.push({ diff: s, match: 1 });
                diffs.push({ diff })
            }
            return diffs
        }
        function findDiff() {
            var e = document.querySelectorAll('.editor');
            var str1 = e[0].innerText.trim(), str2 = e[1].innerText.trim();
            if (str1 && str2) {
                var diffs = getDiff(str1, str2);
                console.log(diffs)
                e[1].innerHTML = diffs.map(i => i.match ? i.diff : `<font color=red>${i.diff}</font>`).join('')
            }
        }
    </script>
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月25日

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!