• Javascript——JS常用的方法(二)常用字符串方法


    1.字符串长度

    length 属性返回字符串的长度:

    var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var sln = txt.length;
    console.log(sln)// 26
    
    • 1
    • 2
    • 3

    2.indexOf() 方法返回字符串中指定文本首次出现的索引(位置)

    ⚠️如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。

    var str = "The full name of China is the People's Republic of China.";
    var pos = str.indexOf("China");
    console.log(pos)//17
    
    **lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:**
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.slice() 方法

    slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。
    该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。

    提取部分字符串

    有三种提取部分字符串的方法:
    slice(start, end)
    substring(start, end)
    substr(start, length)

    var str = "Apple, Banana, Mango";
    var res = str.slice(7,13);console.log(res)//Banana
    
     - 如果某个参数为负,则从字符串的结尾开始计数。
     - 如果省略第二个参数,则该方法将裁剪字符串的剩余部分:
     var res = str.slice(7);console.log(res)//Banana
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.substring() 方法

    substring() 类似于 slice()。
    不同之处在于 substring() 无法接受负的索引。

    var str = "Apple, Banana, Mango";
    var res = str.substr(7,6);console.log(res)//Banana
    
    • 1
    • 2

    5.substr() 方法

    substr() 类似于 slice()。
    不同之处在于第二个参数规定被提取部分的长度。

    6.替换字符串内容 ,String. replace() 方法用另一个值替换在字符串中指定的值:

    replace() 方法不会改变调用它的字符串。它返回的是新字符串。
    默认地,replace() 只替换首个匹配:

    str = "a b c d!";
    var n = str.replace("c", "f");console.log(ns)//a b f d!
    
    • 1
    • 2

    7.转换为大写和小写,通过 String.toUpperCase() 把字符串转换为大写,String.toLowerCase() 把字符串转换为小写

    8.String.concat() 方法连接两个或多个字符串

    ⚠️所有字符串方法都会返回新字符串。它们不会修改原始字符串。
    正式地说:字符串是不可变的:字符串不能更改,只能替换。

    var text1 = "H";
    var text2 = "W";
    text3 = text1.concat(" ",text2);console.log(text3)//HW
    
    • 1
    • 2
    • 3

    9.String.trim()方法删除字符串两端的空白符

    undefined
    var str = "       He Wo        ";
    console.log(str.trim())//He Wo
    
    也可搭配正则表达式使用 replace() 方法代替:
    if (!String.prototype.trim) {
      String.prototype.trim = function () {
        return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
    };
    var str = "       He Wo        ";console.log(str)//He Wo
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    10.提取字符串字符;

    两个提取字符串字符的安全方法:
    String.charAt(position)
    String.charCodeAt(position)

    String.charAt() 方法返回字符串中指定下标(位置)的字符串:

    var str = "HELLO WORLD";
    str.charAt(0);            // 返回 H
    
    • 1
    • 2

    String.charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码:

    var str = "HELLO WORLD";
    
    str.charCodeAt(0);         // 返回 72
    
    • 1
    • 2
    • 3

    12.把字符串转换为数组,可以通过 String.split() 将字符串转换为数组:

    var txt = "a,b,c,d,e";   // 用逗号分隔
    console.log(txt.split(","))//['a', 'b', 'c', 'd', 'e']
    
    var txt = "a,b,c,d,e";     // 用空格分隔
    console.log(txt.split(" ")) //['a,b,c,d,e']
    
    var txt = "a,b,c,d,e";   // 用竖线分隔
    console.log(txt.split(",")) //['a,b,c,d,e']
    
    **如果分隔符是 "",被返回的数组将是间隔单个字符的数组:**
    var txt = "Hello";       // 字符串
    console.log(txt.split(""))//['H', 'e', 'l', 'l', 'o']
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    13.用于搜索字符串的 JavaScript 方法: String.indexOf() 方法返回指定文本在字符串中第一次出现(的位置)的索引。

    String.lastIndexOf()方法
    lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引

    如果未找到文本,indexOf() 和 lastIndexOf() 都返回 -1:

    var str="Hello";
    var n=str.indexOf("e");console.log(n)//1
    
    • 1
    • 2

    14.String.search() 方法在字符串中搜索指定值并返回匹配的位置

    let str = "Please locate";
    str.search("locate")     // 返回 7
    
    • 1
    • 2

    15.String.match() 方法根据正则表达式在字符串中搜索匹配项,并将匹配项作为 Array 对象返回。

    let text = "The rain in SPAIN stays mainly in the plain";
    text.match(/ain/g)    // 返回数组 [ain,ain,ain]
    
    • 1
    • 2

    16.String.includes()如果字符串包含指定值,includes() 方法返回 true。没找到返回false

    **语法:
    string.includes(searchvalue, start)
    参数一: 必需。需要搜索的字符串
    参数二: 可选。默认为 0. 开始搜索的位置。
    **

    let text = "Hello world";
    text.includes("world")    // 返回 true
    
    
    • 1
    • 2
    • 3

    17.字符串模板。

    模板字面量使用反引号 (``)

    let text = `He's often called "Johnny"`;
    
    • 1

    字符串插值(string interpolation):${…}

    变量替换:
    let firstName = "Bill";
    let lastName = "Gates";
    let text = `Welcome ${firstName}, ${lastName}!`;//Welcome Bill, Gates!
    
    
    表达式替换:
    let price = 10;
    let VAT = 0.25;
    let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;
    console.log(total)//Total: 12.50
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    深圳xxx公司测试岗位企业面试题
    1135 Is It A Red-Black Tree
    极坐标扇图使用场景与功能详解
    电力现货价格的高效建模和预测(R实现)
    【CSS】
    事务、定时任务、多线程
    Web基础与http协议
    java springboot测试类鉴定虚拟MVC运行值与预期值是否相同
    getenv、setenv和putenv实践
    设计模式-01-单例设计模式
  • 原文地址:https://blog.csdn.net/weixin_43551242/article/details/128092625