如题:
string str1="123456";
string str2="123abc45ef6ghi";
如何得到str2的abc,ef,ghi.
C#字符串对比,并且取出其中不同的字符串
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- CSDN专家-showbo 2022-03-25 16:58关注
大概写了个js,C#题主可以按照js代码形式改下,逻辑差不多
<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>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
- ¥15 使用MATLAB进行余弦相似度计算加速
- ¥15 服务器安装php5.6版本
- ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
- ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
- ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
- ¥15 arcgis处理土地利用道路 建筑 林地分类
- ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
- ¥100 寻会做云闪付tn转h5支付链接的技术
- ¥15 DockerSwarm跨节点无法访问问题