Home > Web Front-end > JS Tutorial > body text

js implements repeated splicing of multiple strings at the beginning and end

小云云
Release: 2018-03-17 11:10:12
Original
2639 people have browsed it

This article mainly shares with you js to realize the splicing of multiple strings with repeated start and end, string array, repeated start and end of strings, removal of repeated parts, and splicing. I hope it can help everyone.

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);
Copy after login
rrree

The above is the detailed content of js implements repeated splicing of multiple strings at the beginning and end. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template