日期:2014-05-16  浏览次数:20524 次

js 两个以“,”分割的字符串去重,然后组合
var astr = "a,b,c,d,"
var bstr = "a,c,f,e,w";
我想要的结果是 "a,b,c,d,f,e,w"
请高手帮忙

------解决方案--------------------
JScript code

var astr = "a,b,c,d";
var bstr = "a,c,f,e,w"; 

var s = "," + astr + ","
var a  = bstr.split(",");
for(var i=0;i<a.length;i++){
   if(s.indexOf(","+ a[i] +",")==-1){
      s += a[i] + ",";
   }    
}
s = s.substr(1,s.length-2);
alert(s)

------解决方案--------------------
[Quote=引用:]
var astr = "a,b,c,d, "
var bstr = "a,c,f,e,w ";
我想要的结果是 "a,b,c,d,f,e,w "
请高手帮忙
[/Quote]
var astr = "a,b,c,d, "
var bstr = "a,c,f,e,w ";
var arr = bstr.split( ", ");
for(var i=0;i <arr.length;i+=1){
if(!astr.contains(arr[i]))
astr += ", "+arr[i];
}
------解决方案--------------------
JScript code
 
<script type="text/javascript">
<!--
var astr = "abcd";
var bstr = "a,c,f,e,w";
for(var i = 0, result = bstr.replace(/,/g,''); i < result.length; i++)
(astr.indexOf(result.charAt(i)) == -1) && (astr+=result.charAt(i));
alert(astr.split(''))

//-->
</script>

------解决方案--------------------
JScript code

var astr = "a,b,c,d," 
var bstr = "a,c,f,e,w"; 
var b= bstr .split(",");
for(var   i=0;i <b.length;i+=1){ 
  if(! astr.contains(b[i])) 
      astr   += ", "+b[i];