javascript_字符操作_ZHOU125disorder_

字符操作

  • length(字符长度)和indexOf(正向搜索字符)
	    <script>
        //字符串操作方法
        //什么是字符串凡是被""或''包含的东西叫字符串
        //1_length(字符长度)
        //2_正向搜索(寻找)
        var value="我的名字叫卡卡西年龄30我的愿望是成为火影"
        console.log(value);//(返回我的名字叫卡卡西年龄30我的愿望是成为火影)
        console.log(value.length);//空格也算(返回21)
        console.log(value.indexOf("我"));//正向搜索(返回0)
        console.log(value.indexOf("我的"));//找到第一个并返回下标值(返回0)
        console.log(value.indexOf("我卡卡西"));//整体字符串中有但是没有连在一起(返回-1)
        console.log(value.indexOf("我爱罗"));//没找到(返回-1)
        console.log("------------------------------------------------------------");
    </script>
  • lastIndexOf(反向搜索字符)
        //3_lastIndexOf
        var value="我的名字叫卡卡西年龄30我的愿望是成为火影"
        console.log(value.lastIndexOf("火影"));//从后往前找并返回第一个字符的下标(返回19)
        console.log(value.lastIndexOf("我爱罗"))//没找到(返回-1)
        console.log(value.lastIndexOf("我卡卡西"));//整体字符串中有但是没有连在一起(返回-1)
  • slice(截取字符串)
        //4_slice(截取字符串)可以是一个或者两个参数     也可以接收负数  
        var value="我的名字叫卡卡西年龄30我的愿望是成为火影"
        console.log(value.slice(1));//返回字符串中该下标到结束的位置(的名字叫卡卡西年龄30我的愿望是成为火影)
        console.log(value.slice(5,8));//从开始下标到结束前一个的下标包头不包尾(卡卡西)
        console.log(value.slice(0.2));//当是小数是取整进行正常操作(我的名字叫卡卡西年龄30我的愿望是成为火影)
        console.log(value.slice(-6,-4));//从后往前包头不包尾后为头(望是)
        console.log(value.slice(-5));//从后往前数从下标+1的位置开始到最后的位置(是成为火影)
  • substr和substring(截取字符串)
        //6_substr(截取字符串)    第一个参数代表初始位值,第二个参数代表截取长度
        var value="我的名字叫卡卡西年龄30我的愿望是成为火影"
        console.log(value.substr(0,8));//(我的名字叫卡卡西);
        console.log(value.substr(-2,3));//从后往前包头不包尾后为头(火影);
        console.log(value.sub(1));//无长度输出全部(<sub>我的名字叫卡卡西年龄30我的愿望是成为火影</sub>)
        console.log(value.sub(-2));//无长度输出全部(<sub>我的名字叫卡卡西年龄30我的愿望是成为火影</sub>)
  • replace(替换)_toUpperCase(大写)_toLowerCase(小写)
        //7_replace(替换)
        console.log(value.replace("卡卡西","我爱罗"));//(我的名字叫我爱罗年龄30我的愿望是成为火影)
        //8_toUpperCase(大写)       tolowerCase(小写) 
        var content="i love you";
        console.log(content.toUpperCase());//(I LOVE YOU)
        var value="I LOVE YOU";
        console.log(value.toLowerCase());//(i love you)
  • trim(删除空白的部分)_split(分割字符串);
        //9_trim(删除空白部分)      兼容性问题        IE8以及以下不支持
        var content = "         卡卡西          ";
        console.log(content.trim());//(卡卡西); 
        //10_split(分割)
        var hope="i belive i can fly"
        console.log(hope.split("",1));//全部分割长度为1
        console.log(hope.split(" ",1));//以一个空格为分割线长度为1

使用字符串的方法写一个轮播图

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符串轮播图</title>
    <style>
        *{
            margin: 0px;
            padding: 0px;
            list-style: none;
        }

        html,body{
            width: 100%;
            height: 100%;
        }

        .banner{
            width: 356px;
            height: 456px;
            margin: 49px auto;
            box-shadow: 0 0 1px #096;
            position: relative;
        }

        .banner ul{
            width: 100px;
            height: 456px;
            box-shadow: 0 0 1px red;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            text-align: center;
        }
        ul li{
            width: 100px;
            height: 150px;
            border-bottom: 1px solid #096;
            cursor: pointer;
        }

        img{
            width: 256px;
            height: 456px;
            position: absolute;
            top: 0;
            left: 100px;
            opacity: 0;
        }
        .cls{
            background-color: #096;
        }

        .ddd{
            background-color: #ddd;
        }
        button{
            width: 18px;
            height: 36px;
            position: absolute;
            background-color: #ddd;
            opacity: .5;
            outline: none;
            top: 50%;
        }

        button:hover{
            opacity: 1;
        }
        button:nth-of-type(1){
            left: 100px;
        }
        button:nth-of-type(2){
            right: 0px;
        }

        ol{
            width: 72px;
            display: flex;
            flex-direction: row;
            justify-content: space-around;
            position: absolute;
            bottom: 0;
            right: 10px;
        }

        ol li{
            width: 8px;
            height: 8px;
            border: 1px solid #ddd;
            background-color: #096;
            border-radius: 50%;
        }

        ol li:hover{
            background-color: #ddd;
        }
        </style>
        <div class="banner">
            <ul>
                <li class="cls">鞠婧祎</li>
                <li>鞠婧祎</li>
                <li>鞠婧祎</li>
            </ul>
            <img src="img/1.jpg" alt="" style="opacity: 1;">
            <img src="img/2.jpg" alt="">
            <img src="img/3.jpg" alt="">
            <button><</button>
            <button>></button>
            <ol>
                <li class="ddd"></li>
                <li></li>
                <li></li>
            </ol>
        </div>
    <script>
        //使用2维数组存储img的src
        var imgs_src=[
            ["img/1.jpg","img/2.jpg","img/3.jpg"],
            ["img/4.jpg","img/5.jpg","img/6.jpg"],
            ["img/7.jpg","img/8.jpg","img/9.jpg"]
        ]
        // console.log(imgs_src);
        //获取ul下的li
        var banner=document.getElementsByClassName("banner")[0];
        // console.log(banner);
        var ul=document.getElementsByTagName("ul")[0];
        var ul_li=ul.getElementsByTagName("li");
        //获取button
        var btn=document.getElementsByTagName("button");
        var imgs=document.getElementsByTagName("img");
        var ol=document.getElementsByTagName("ol")[0];
        var ol_li=ol.getElementsByTagName("li");
        var num=0;
        var timer;
        for(var i=0;i<ul_li.length;i++){
            ul_li[i].index=i;
            ul_li[i].onclick=function(){
                // alert("卡卡西");
                for(var j=0;j<ul_li.length;j++){
                    ul_li[j].className="";
                }
                for(var x=0;x<imgs_src.length;x++){
                    imgs[x].src=imgs_src[this.index][x];
                }
                this.className="cls";
            }
        }

        //button点击事件
        for(var m=0;m<btn.length;m++){
            //第1个btn
            btn[0].onclick=function(){
                // alert("卡卡西");
                num--;
                if(num<0){
                    num=imgs.length-1;
                }
                for(var n=0;n<imgs.length;n++){
                    imgs[n].style.opacity="0";
                    ol_li[n].className="";
                }
                imgs[num].style.opacity="1";
                ol_li[num].className="ddd";
            }
            //第2个btn
            btn[1].onclick=function(){
                num++;
                if(num>imgs.length-1){
                    num=0;
                }
                for(var n=0;n<imgs.length;n++){
                    imgs[n].style.opacity="0";
                    ol_li[n].className="";
                }
                imgs[num].style.opacity="1";
                ol_li[num].className="ddd";
            }
        }

        //分页器的点击事件
        for(var y=0;y<ol_li.length;y++){
            ol_li[y].zhi=y;
            ol_li[y].onclick=function(){
                    for(var p=0;p<ol_li.length;p++){
                        ol_li[p].className="";
                        imgs[p].style.opacity="0";
                    }
                this.className="ddd";
                imgs[this.zhi].style.opacity="1";
            }
        }


        var mouser=function(){
            num++;
                if(num>imgs.length-1){
                    num=0;
                }
                for(var n=0;n<imgs.length;n++){
                    imgs[n].style.opacity="0";
                    ol_li[n].className="";
                }
                imgs[num].style.opacity="1";
                ol_li[num].className="ddd";
        }

        var timer=setInterval(mouser,1200);
        banner.onmouseover=function(){
            clearInterval(timer);
        }

        banner.onmouseout=function(){
             timer=setInterval(mouser,1200)
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请解释分析下面这段程序:%%%无序充电投标 clear clc load data_disorder Pch=[Pch_CS1_disorder;Pch_CS2_disorder;Pch_CS3_disorder;Pch_CS4_disorder];%充电站充电功率 %市场出清问题 Link=zeros(24,96);%时段换算矩阵(日前1h换算为实时15min) for i=1:24 Link(i,4*i-3:4*i)=1; end Loadcurve=[0.955391944564747,0.978345604157644,1,0.995019488956258,0.972932005197055,0.970333477695972,0.930489389346037,0.890428757037679,0.902771762667822,0.941966219142486,0.911000433087917,0.862061498484192,0.840190558683413,0.831095712429623,0.756604590731919,0.671719359029883,0.611520138588133,0.582936336076224,0.572542226071893,0.574707665656128,0.587267215244695,0.644218276310091,0.755521870939801,0.884798614118666]; Loadcurve=Loadcurve*Link;%换成96个时段 PL_base=[5.704;5.705;5.631;6.518;4.890;5.705;5.847]*1000;%负荷分布 PL=PL_base*Loadcurve;%基础负荷(负荷曲线从08:00开始算起,即第9个时段) Pf=sdpvar(7,96);%馈线功率 Pf(1,:)=PL(1,:)+Pch(1,:);Pf(2,:)=PL(2,:);Pf(3,:)=PL(3,:);Pf(4,:)=PL(4,:)+Pch(2,:);Pf(5,:)=PL(5,:)+Pch(3,:);Pf(6,:)=PL(6,:);Pf(7,:)=PL(7,:)+Pch(4,:);%馈线功率组成 Pg=sdpvar(10,96);%发电商分段电量 Pg_step=1000*[20,5,3,2,2,2,2,2,2,inf]';%报价区间 Price_DSO=[3:12]'*0.1;%分段电价 Obj=0.25*sum(sum((Price_DSO*ones(1,96)).*Pg));%目标为用电费用最小 Constraint=[0<=Pg<=Pg_step*ones(1,96),sum(Pg)==sum(Pf)];%约束条件 optimize(Constraint,Obj);%求解线性规划问题 Pg=double(Pg);%发电机功率 Pf=double(Pf);%馈线功率 isPg=(Pg>0);%为了计算出清电价,计算发电机分段选择情况 DLMP=sum(isPg)/10+0.2;%出清电价计算 %绘图 figure(1)%节点边际电价 stairs(DLMP); xlabel 时间 ylabel 电价(元/kWh) ylim([0.3,1.3]) figure(2)%负荷曲线 hold on plot(sum(PL)/1000); plot(sum(Pf)/1000,'r.-'); xlabel 时间 ylabel 负荷(MW) legend('基础负荷','无序充电负荷') Cost=sum(sum(Pch).*DLMP);%总用电费用 result_disorder.Cost=Cost;result_disorder.DLMP=DLMP;result_disorder.Pf=Pf;result_disorder.Pg=Pg;%结果保存 save('result_disorder','result_disorder');
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值