首页 > 代码库 > 字符串转数组

字符串转数组

将字符串按某个字符切割成若干个字符串,并以数组形式返回

var s = "abc,abcd,aaa";
ss = s.split(",");  // 在每个逗号(,)处进行分解。
console.log(ss);  // ["abc", "abcd", "aaa"]

 

截取数组中_720替换为_360:

function changeLargeImg(self) {
    imgClear();
    $("section").show();
    // 大图
    var dataLargeSrc = http://www.mamicode.com/$(self).attr("data-largesrc");
    dataLargeSrc = dataLargeSrc.split(",");
    var topHtml = "<div class=‘swiper-wrapper‘>";
    for(var i in dataLargeSrc){
        topHtml+= "<div class=‘swiper-slide‘ style=‘background-image:url("+dataLargeSrc[i]+")‘></div>";
    }
    topHtml += "</div>";
    $(".gallery-top").append(topHtml);
    var btn =  "<div class=‘swiper-button-next swiper-button-white‘></div><div class=‘swiper-button-prev swiper-button-white‘></div>";
    $(".gallery-top").append(btn);
    // 克隆,小图
    var dataSrc =http://www.mamicode.com/ dataLargeSrc.slice();
    var data360Src =http://www.mamicode.com/ [];
    for (var i in dataSrc) {
        data360Src.push(dataSrc[i].replace(/_720/, "_360")); //replace找到并替换,并返回新的数组,push放入新的数组中;
    }
    var thumbsHtml = "<div class=‘swiper-wrapper‘>";
    for(var i in data360Src){
        thumbsHtml+= "<div class=‘swiper-slide‘ style=‘background-image:url("+data360Src[i]+")‘></div>";
    }
    thumbsHtml += "</div>";
    $(".gallery-thumbs").append(thumbsHtml);

    // swiper图片动效启动
    var galleryTop = new Swiper(‘.gallery-top‘, {
        nextButton: ‘.swiper-button-next‘,
        prevButton: ‘.swiper-button-prev‘,
        spaceBetween: 10
    });
    var galleryThumbs = new Swiper(‘.gallery-thumbs‘, {
        spaceBetween: 10,
        centeredSlides: true,
        slidesPerView: ‘auto‘,
        touchRatio: 0.2,
        slideToClickedSlide: true
    });
    // 双向控制
    galleryTop.params.control = galleryThumbs;
    galleryThumbs.params.control = galleryTop;
}

 

字符串转数组