当前位置:Gxlcms > JavaScript > js实现多个字符串首尾重复拼接

js实现多个字符串首尾重复拼接

时间:2021-07-01 10:21:17 帮助过:11人阅读

本文主要和大家分享js实现多个字符串首尾重复进行拼接,字符串数组,字符串之间首尾重复,去除重复部分,进行拼接,希望能帮助到大家。

var j=0;
var isRepeat = null
var index = null

var res = new Array()
var tmp_key2
var tmp_key1 
var Repeat = function(ary) {

    var arr1 = tmp = ary;
    for(var i=0; i<ary.length; i++){

        tmp_key1 = ary[i];
        var len1 = tmp_key1.length;
        ccc:
        for(j = 0 ; j<arr1.length; j++){
            if(i===j){
              continue;
            }

            tmp_key2 = arr1[j];
            var len2 = tmp_key2.length;


            index = -1;
            var index_num =0;

            for(var k = 0; k < len2; k++) {

                var tmp_str = tmp_key2.slice(0,k+1);


                index = tmp_key1.indexOf(tmp_str);

                if(index !== -1){
                    isRepeat = index;
                } 
                if( isRepeat != null && index ===-1 ){

                    break ccc;
                }

            }



        }
        // console.log(isRepeat);

        if(isRepeat !== null) { 

            res.push(tmp_key1.slice(0,isRepeat)+tmp_key2)

        } 
        index = null;
        isRepeat = null;
    }
    console.log(res);
    return res;
}

var ary = new Array("中国红","红红火火","zhanglei","lei@silenc","kkkzhang");
Repeat(ary);
["中国红红火火", "zhanglei@silenc", "kkkzhanglei"]

以上就是js实现多个字符串首尾重复拼接的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行