JS实现为排序好的字符串找出重复行的方法
作者:yenange 时间:2024-04-10 10:49:53
本文实例讲述了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程序设计有所帮助。
标签:JS,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL数据表添加字段的三种方式
2024-01-13 10:49:30
torch.utils.data.DataLoader与迭代器转换操作
2021-01-18 11:02:34
![](https://img.aspxhome.com/file/2023/3/128543_0s.png)
mysql limit分页优化方法分享
2024-01-26 22:03:40
详解Python编程中time模块的使用
2023-08-29 16:57:08
对python 自定义协议的方法详解
2023-05-26 07:20:04
Python绘制散点图之可视化神器pyecharts
2022-03-28 18:46:02
![](https://img.aspxhome.com/file/2023/9/92189_0s.png)
js中鼠标滚轮事件详解
2010-02-05 12:20:00
![](https://img.aspxhome.com/file/UploadPic/20102/5/wheelchrome-82s.png)
python中if嵌套命令实例讲解
2022-03-11 21:52:51
python实现机器人卡牌
2023-05-29 17:27:38
![](https://img.aspxhome.com/file/2023/3/64713_0s.gif)
djang常用查询SQL语句的使用代码
2022-04-27 02:59:51
![](https://img.aspxhome.com/file/2023/4/110244_0s.png)
javascript 常见汉字转换成拼音工具
2008-03-03 16:54:00
![](https://img.aspxhome.com/file/UploadPic/20083/3/hanzi-pinyin_98s.jpg)
Python标准库inspect的具体使用方法
2023-05-30 08:00:37
JavaScript高级程序设计(第3版)学习笔记2 js基础语法
2024-06-07 15:59:19
详细介绍Python进度条tqdm的使用
2022-12-13 19:45:08
![](https://img.aspxhome.com/file/2023/9/77619_0s.gif)
使用Kivy将python程序打包为apk文件
2021-01-17 21:04:11
![](https://img.aspxhome.com/file/2023/6/123686_0s.png)
innerHTML在Mozilla Firefox和Opera下执行的一个特例情况。
2023-09-16 11:23:46
详解Python中find()方法的使用
2022-08-25 04:25:53
InnoDB解决幻读的方法详解
2024-01-15 13:50:53
![](https://img.aspxhome.com/file/2023/8/122888_0s.jpg)
使用Python实现控制摄像头的方法详解
2023-01-15 14:38:12
asp如何计算下载一个文件需要多长时间?
2009-11-25 20:17:00