This article mainly introduces the method of JS implementation to find duplicate lines for sorted strings, involving JavaScript string operation related skills, which has certain reference value. Friends in need can refer to it
To realize such a requirement, in an Editplus document, there are many rows of 10-digit numbers, and these numbers have been sorted.
For example:
1234567890
1234567891
1234567892
1234534124
1234614124
4321412414
5636373573
Is there any way to make it easier? Find two lines with at least the first 7 digits that are the same? [ Recommended related tutorials】
1.
JavaScript video tutorial
2.
JavaScript online manual
3.
<!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"> p{ float:left; } #pCenter{ 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> <p> 请输入数值:<br /> <textarea id="txtOldData" class="txt"> 1234567890 1234567891 1234567892 1234534124 1234614124 4321412414 5636373573 </textarea> </p> <p style="padding-top:90px;padding" > <input type="button" value="测试==>" onclick="test()" /> </p> <p> 结果:<br /> <textarea id="txtAnswer" class="txt"></textarea> </p> </body> </html>