源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JS实现为排序好的字符串找出重复行的方法

  • 时间:2020-04-17 03:09 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JS实现为排序好的字符串找出重复行的方法
本文实例讲述了JS实现为排序好的字符串找出重复行的方法。分享给大家供大家参考,具体如下: 实现这样一个需求,在一个Editplus文档中,有很多行10位的数字,这些数字已经排好序了。 比如: 1234567890 1234567891 1234567892 1234534124 1234614124 4321412414 5636373573 有什么办法能方便的找出两行至少前7位相同的数字吗? 比如,上面的数字中,能够找出 1234567890 1234567891 1234567892
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title></title>
  <style type="text/css">
    div{ float:left; }
    #divCenter{ padding-top:100px;margin:0 50px; }
    .txt{width:200px;height:200px;}
    #txtOldData{background-color:#A6CAF0;}
    #txtAnswer{background-color:#EBA9A6;}
  </style>
  <script type="text/javascript">
    function test() {
      var arr = document.getElementById("txtOldData").value.replace(/ +/g, '').split("\n");
      var tempStr = arr[0].substring(0, 7);
      var compareLen = 7, equalNum = 0;
      var answer = "";
      for (var i = 1; i < arr.length; i++) {
        if (arr[i].substring(0, 7) == tempStr) {
          if (equalNum == 0)
            answer += arr[i - 1] + "\n";
          answer += arr[i] + "\n";
          equalNum++;
        } else {
          tempStr = arr[i].substring(0, 7);
          equalNum = 0;
        }
      }
      document.getElementById("txtAnswer").value = (answer);
    }
  </script>
</head>
<body>
  <div>
    请输入数值:<br />
    <textarea id="txtOldData" class="txt">
1234567890
1234567891
1234567892
1234534124
1234614124
4321412414
5636373573
    </textarea>
  </div>
  <div style="padding-top:90px;padding" >
    <input type="button" value="测试==>" onclick="test()" />
  </div>
  <div>
    结果:<br />
    <textarea id="txtAnswer" class="txt"></textarea>
  </div>
</body>
</html>

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/472.htm]JavaScript查找算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/462.htm]JavaScript动画特效与技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/439.htm]JavaScript错误与调试技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/297.htm]JavaScript数据结构与算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/281.htm]JavaScript遍历算法与技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/119.htm]JavaScript数学运算用法总结[/url]》 希望本文所述对大家JavaScript程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部