• 各种检索字符串方法


    indexOf(小程序不支持,不支持,不支持)

    检索是否包含某字符串,返回值是首次找到的当前字符的下标,没找到返回-1

    var str = "hello world";
    console.log(str.indexOf("hello")); ====>0
    console.log(str.indexOf("World")); ====>-1
    console.log(str.indexOf("world")); ====>6

    search

    跟indexof很像,但是search可以用正则检索

    var str="hello World";
    console.log(str.search(/World/)); ====>6
    console.log(str.search(/world/)); ====>-1
    console.log(str.search(/world/i); ====>6

    match

    方法能够找出所有匹配的子字符串,并以数组的形式返回。

    如果没有找到匹配字符,则返回 null,而不是空数组。

    var s = "http://c.biancheng.net";
    var a = s.match(/c/n);  //全局匹配所有字符c
    console.log(a);  //返回数组[c,c]。

    str.includes('')

    有返回true,没有返回false。也不用为记住-1而发愁了

    let str = 'Hello World!';
    console.log(str.includes('H'));//true
    console.log(str.includes('a'));//false

    startsWith()

    判断该字符串是否为某个字符串的首位。有就是true,不是就是false。

    endsWith()和startsWith()相反。判断是否为末尾。

    let str = 'Hello World!';
    console.log(str.startsWith('H'));//true
    console.log(str.startsWith('Hello'));//true
    console.log(str.startsWith('e'));//false
    let str = 'Hello World!';
    console.log(str.endsWith('!'));//true
    console.log(str.endsWith('d!'));//true
    console.log(str.endsWith('e'));//false

    这三个方法都支持第二个参数,表示看是搜索的位置。

    let str = 'Hello World!';
    console.log(str.includes('World',5));//true 从索引5(包含索引5)开始搜索
    console.log(str.includes('World',7));//false
    console.log(str.startsWith('lo',3))//true
    console.log(str.startsWith('H',3));//false
    console.log(str.endsWith('el',3));//true 
    endsWith()和上面两个不一样,它的第二个参数代表前几个。“Hel”,所以返回true
  • 相关阅读:
    webpack(二)
    webpack(一)
    初探Vue SSR(1)
    Node版本管理控制器n
    Gitlab用户在组中有五种权限:Guest、Reporter、Developer、Master、Owner
    微信小程序分享参数传递
    关于vue-cli3.*搭建项目遇到问题整理
    请求头出现provisional headers are shown 和 vue-cli 3.x配置跨域代理
    HDU6409 没有兄弟的舞会
    HDU6446 Tree and Permutation
  • 原文地址:https://www.cnblogs.com/ll15888/p/11600501.html
Copyright © 2020-2023  润新知