zoukankan      html  css  js  c++  java
  • 各种检索字符串方法

    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
  • 相关阅读:
    联合索引和多个单列索引选择
    CentOS6.5 一台服务器同时安装多个Mysql数据库
    一次CentOS的服务器被攻击教训
    java版本的memcache静态化
    mysql存储空间满的处理方式
    MariaDB 10.0 和 MariaDB 10.1 存储过程中 PREPARE FROM EXECUTE 区别
    CentOS6.x 优化脚本
    Mysql 使用 “LOAD DATA INFILE”需要注意的问题
    Mysql 日期类型比较 TIMESTAMPDIFF
    CentOS6.x 源码安装Nginx
  • 原文地址:https://www.cnblogs.com/ll15888/p/11600501.html
Copyright © 2011-2022 走看看