博主信息
博文 16
粉丝 0
评论 0
访问量 12698
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
4.07字符串与数组的一些常用方法
Blastix Riotz
原创
745人浏览过
  1. concat()拼装
    let str = "html".concat("css","js","php","!",888);
    htmlcssjsphp!888
  2. slice(start,end)取子串
    1. str = "hello php.cn";
    2. let res = str.slice(0,5);
    3. console.log(res);//hello
    4. res = str.slice(6);
    5. console.log(res);//php.cn
    3.substr(start,size)取子串
    1. res = str.substr(0,5);
    2. console.log(res);//hello
    4.trim():删除字符串两边的空白字符
    1. let pw = " 888 ";
    2. console.log(pw.length);//11
    3. console.log(pw.trim());//888
    5.split() 将字符串打散成数组
    1. res = str.split("");
    2. console.log(res);//["h", "e", "l", "l", "o", " ", "p", "h", "p", ".", "c", "n"]
    3. let email = "demo@mail.com";
    4. res = email.split("@");
    5. console.log(res);//["demo", "mail.com"]
    6. console.log('userName = ',res[0]);//userName = demo
    7. console.log('emailAddress = ',res[1]);//emailAddress = mail.com
    6.replace() 替换字符串内容
    1. res = email.replace("demo","admin")
    2. console.log(res);// admin@mail.com

    数组方法

    1.pop() 从数组中删除最后一个元素
    1. arr = ["Banana", "Orange", "Apple", "Mango"];
    2. arr.pop();
    3. console.log(arr);// ["Banana", "Orange", "Apple"]
    2.push() 在结尾处向数组添加一个新的元素
    1. arr.push("Cherry");
    2. console.log(arr);// ["Banana", "Orange", "Apple", "Cherry"]
    3.shift() 删除首个数组元素
    1. arr.shift();
    2. console.log(arr);// ["Orange", "Apple", "Cherry"]
    4.unshift() 将新元素添加到数组的开头
    1. arr.unshift("Lemon");
    2. console.log(arr);// ["Lemon", "Orange", "Apple", "Cherry"]
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学