How to check two string have same characters including special characters

后端 未结 6 1054
陌清茗
陌清茗 2021-01-25 04:41

I have two question

1) how can I check two shuffle string have same characters Like I have

var str1 = \"ansar@#//1\";
var str2 = \"@#//sanra1\";
         


        
6条回答
  •  迷失自我
    2021-01-25 05:23

    this will return empty string if character set in both strings is the same.

    function findDiff (str1, str2)
    {
        var diff = '';
    
        if (str1.length > str2.length)
        {
            var search = str1;
            var compare = str2;
        }
        else
        {
            var search = str2;
            var compare = str1;
        }
    
        for (var i = 0; i < search.length; i++)
        {
            var symbol = search[i];
    
            if (compare.indexOf(symbol) === -1)
            {
                diff += symbol;
            }
        }
        return(diff);
    }
    
    findDiff("ansar@#//123", "@#//sanra");
    

    https://jsfiddle.net/tadaspaplauskas/pn7jnj8e/

提交回复
热议问题