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
  • 相关阅读:
    字符串复习笔记
    构造
    网络流复习笔记
    LCT学习笔记
    省选前的数据结构训练
    在windows安装并启动测试kafka
    Scala 原生操作MySQL
    Scala基础语法
    ORACLE查出表所有的触发器及触发器详细信息
    转载二,JAVA面试题
  • 原文地址:https://www.cnblogs.com/ll15888/p/11600501.html
Copyright © 2011-2022 走看看