用JS找出两个字符串相同的最大子串

例如

输入:str1 : instritesting

           str2:  string

输出:stri

 

    function Clear(str1,str2){

        if (str1 > str2){
            shorter = str2;
            longer = str1;
        }
        else {
            shorter = str1;
            longer = str2;
        }
        for (var a = shorter.length;a > 0;a--){
            for (var b = 0;a+b < shorter.length;b++){

                String1 = shorter.substring(b,a+b)

                if (longer.indexOf(String1) >= 0){

                    targetstring = String1;

                    return targetstring;
                }
            }
        }

    }

    console.log(Clear("instritesting", "string"))

 

你可能感兴趣的:(用JS找出两个字符串相同的最大子串)