美文网首页
JavaScript String方法

JavaScript String方法

作者: fallfilm | 来源:发表于2020-01-07 17:04 被阅读0次

    废话不多说,直接看代码

    String.startsWith() 返回布尔值,表示参数字符串是否在原字符串的头部。
    let str = "hello world!"
    str.startsWith('Hello') // true
    
    String.endsWith() 返回布尔值,表示参数字符串是否在原字符串的尾部。
    let str = "hello world!"
    str.endsWith('!') // true
    
    String.includes() 返回布尔值,表示是否找到了参数字符串。
    let str = "hello world!"
    str.includes('o') // true
    

    以上三个方法都有第二个参数 n ,表示开始搜索的位置

    let str = "hello world!"
    str.startsWith('world', 6) // true
    str.endsWith('Hello', 5) // true
    str.includes('Hello', 6) // false
    

    要注意的是,startsWith,includes,是从n的位置开始往后检索,直到字符串结束;endsWith 是针对前n个字符!

    String.repeat(n) 方法返回一个新字符串,表示将原字符串重复n次。
    let str = 'abc'
    str.repeat(3)  // abcabcabc
    

    写到这里突然想到一个骚操作!清空字符串 是不是可以这样 str.repeat(0)

    如果参数是小数,会被取整

    let str = 'abc'
    str.repeat(3.8)  // abcabcabc
    str.repeat(2.1)  // abcabc
    

    如果repeat的参数是负数或者Infinity,会报错。
    如果参数是 0 到-1 之间的小数,则等同于 0,这是因为会先进行取整运算。0 到-1 之间的小数,取整以后等于-0,repeat视同为 0。
    参数NaN等同于 0。
    如果repeat的参数是字符串,则会先转换成数字。

    let str = 'abc'
    str.repeat('abc') // ""
    str.repeat('3') // abcabcabc
    str.repeat(NaN) // ""
    str.repeat(Infinity)  // RangeError
    str.repeat(-1)  // RangeError
    str.repeat(-0.9) // ""
    
    String.padStart() 用于头部补全
    'x'.padStart(5, 'ab') // 'ababx'
    'x'.padStart(4, 'ab') // 'abax'
    'xxx'.padStart(2, 'ab') // 'xxx'
    'abc'.padStart(10, '0123456789') // '0123456abc'
    'x'.padStart(4) // '   x'
    
    String.padEnd() 用于尾部补全
    'x'.padEnd(5, 'ab') // 'xabab'
    'x'.padEnd(4, 'ab') // 'xaba'
    'xxx'.padEnd(2, 'ab') // 'xxx'
    'x'.padEnd(4) // 'x   '
    

    上面代码中,padStart()padEnd()一共接受两个参数,第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串。

    String.trimStart()消除字符串头部的空格,trimEnd()消除尾部的空格。它们返回的都是新字符串,不会修改原始字符串。trim() 方法删除字符串两端的空白符
    const s = '  abc  ';
    s.trim() // "abc"
    s.trimStart() // "abc  "
    s.trimEnd() // "  abc"
    

    上面代码中,trimStart()只消除头部的空格,保留尾部的空格。trimEnd()也是类似行为。
    除了空格键,这两个方法对字符串头部(或尾部)的 tab 键、换行符等不可见的空白符号也有效。

    浏览器还部署了额外的两个方法,trimLeft()trimStart()的别名,trimRight()trimEnd()的别名。

    String.indexOf() 返回字符串中指定文本首次出现的索引
    var str = "The full name of China is the People's Republic of China.";
    var pos = str.indexOf("China");  // 17
    
    String.lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引
    var str = "The full name of China is the People's Republic of China.";
    var pos = str.lastIndexOf("China");  // 51
    

    如果未找到文本, indexOf()lastIndexOf() 均返回 -1。
    均有第二个参数n 表示检索的起始位置 indexOf() 从n到结束,lastIndexOf()从n到开始

    String.search() 方法搜索特定值的字符串,并返回匹配的位置
    var str = "The full name of China is the People's Republic of China.";
    var pos = str.search("China");  // 17
    

    两种方法,indexOf()search(),是相等的。
    这两种方法是不相等的。区别在于:
    search() 方法无法设置第二个开始位置参数。
    indexOf() 方法无法设置更强大的搜索值(正则表达式)。

    String.slice() 提取字符串的某个部分并在新字符串中返回被提取的部分
    var str = "Apple, Banana, Mango";
    str.slice(7,13);  // Banana
    str.slice(-13,-7);  // Banana
    str.slice(7);  // Banana, Mango
    str.slice(-13); // Banana, Mango
    

    该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。
    如果某个参数为负,则从字符串的结尾开始计数。
    如果省略第二个参数,则该方法将裁剪字符串的剩余部分,负数从结尾计数。

    String.substring() 类似于 slice()。不同之处在于 substring() 无法接受负的索引。
    String.substr() 类似于 slice()。不同之处在于第二个参数规定被提取部分的长度
    var str = "Apple, Banana, Mango";
    str.substr(7,6); // Banana
    str.substr(7,8); // Banana,M
    

    substr()第二个参数不能为负,因为它定义的是长度。

    String.replace() 方法用另一个值替换在字符串中指定的值
    var str = "Please visit Microsoft!";
    var n = str.replace("Microsoft", "W3School");
    n // Please visit W3School!
    

    replace() 方法不会改变调用它的字符串。它返回的是新字符串。
    默认只替换首个匹配。
    replace() 对大小写敏感。
    如需执行大小写不敏感的替换,使用正则表达式 /i(大小写不敏感)
    如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索)

    var str = "abcABC";
    str.replace(/a/i, "x"); // xbcABC
    str.replace(/A/g, "x"); // abcxBC
    str.replace(/A/gi, "x"); // xbcxBC
    
    String.toUpperCase() 把字符串转换为大写
    var str = "abc"
    str.toUpperCase()  // ABC
    
    String.toLowerCase() 把字符串转换为小写
    var str = "ABC"
    str.toLowerCase()  // abc
    
    String.concat() 连接两个或多个字符串
    var text1 = "a";
    var text2 = "b";
    text1.concat(" ", text2); // a b
    text1.concat(text2); // ab
    text1.concat("-", text2); // a-b
    

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

    String.charAt() 方法返回字符串中指定下标(位置)的字符串 , charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码
    String.split() 将字符串转换为数组
    var str = "a,b,c,d,e,f";
    str.split("|"); // ["a,b,c,d,e,f"]
    str.split(" "); // ["a,b,c,d,e,f"]
    str.split("");  // ["a", ",", "b", ",", "c", ",", "d", ",", "e", ",", "f"]
    

    完!差不多也就这些东西了

    相关文章

      网友评论

          本文标题:JavaScript String方法

          本文链接:https://www.haomeiwen.com/subject/kavractx.html