PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 4.07字符串与数组的一些常用方法

4.07字符串与数组的一些常用方法

Blastix Riotz
Blastix Riotz 原创
2021年04月24日 16:46:20 537浏览
  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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议