javascript 字符串比较函数

来源:互联网 发布:淘宝实名账号出售 编辑:程序博客网 时间:2024/05/21 09:06

比较两个字符串中不同的字符。

例子,php(字符串比较 http://www.jbxue.com/tags/zifuchuanbijiao.html)实现代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <html> <head> <title>php字符串比较_www.jbxue.com</title> </head> <body> <script language = javascript> function diff(){ var s = f.s1.value; var s1 = f.s2.value; document.write("s: " + s + "<br>"); document.write("s1: " + s1 + "<br>"); function sort(s, a){ for(i=0; i<s.length;i++){ a[i] = s.charAt(i); } return a.sort(); } list = sort(s, new Array()); list1 = sort(s1, new Array()); var m = 0; var n = 0; var l = new Array(); for(i = 0; i < list.length; i++){ } for(i = 0; i < list1.length; i++){ } j = 0; while(m < list.length && n < list1.length){ if (list[m] < list1[n]){ l[j] = list[m]; if (m < list.length){ m++; } i++; j++; continue; } if (list[m] > list1[n]){ l[j] = list1[n]; if (n < list1.length){ n++; } i++; j++; continue; } if (list[m] == list1[n]){ if (m < list.length){ m++; } if (n < list1.length){ n++; } i++; continue; } } if (l.length == 0) alert("两个字符串所包含的字符完全一样"); else alert("两个字符串有不同的字符,它们是: " + '"' + l.join(", ") + '"'); } </script> <form name = "f"> <input type = "text" name = "s1"> <input type = "text" name = "s2"> <input type = "button" name = "compare" value = "比较" onclick = "diff()"> </form> </body> </html> 





0 0
原创粉丝点击