javascript 比较两个字符串

来源:互联网 发布:插画网站 知乎 编辑:程序博客网 时间:2024/05/18 12:33

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

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<html>

<head>

<title>Untitled</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>

 

原创粉丝点击