zoukankan      html  css  js  c++  java
  • js字符串操作

    1:concat() 将两个或多个字符的文本组合起来,返回一个新的字符串

    1 var f1="hello";
    2 var f2="world";
    3 document.write(f1.concat(f2))  //hello world

    2:indexof() 返回字符串中一个子串第一处出现的索引。如果没有匹配项,返回 -1

    1 var f3="hello world"
    2 console.log(f3.indexOf('world')) //6
    3 console.log(f3.indexOf('World')) //-1
    4 console.log(f3.indexOf('hello')) //0


    注意:indexOf区分大小写

    3:charAt() – 返回指定位置的字符。  

    1 var f4="hello world";
    2 console.log(f4.charAt(1))   //e
    3 console.log(f4)  //hello world

    4:lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。

    1 var f3="hello world"
    2 console.log(f3.lastIndexOf('world')) //6
    3 console.log(f3.lastIndexOf('World')) //-1
    4 console.log(f3.lastIndexOf('hello')) //0

    5:substring() – 返回字符串的一个子串。传入参数是起始位置和结束位置(不必需)。注:参数不能为负数

    1 var f5="hello world"
    2 console.log(f5.substring(3)) //lo world
    3 console.log(f5.substring(3,8))  //lo wo

    6:match() – 检查一个字符串是否匹配一个正则表达式。 

    7:replace() – 用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。

    8:search() – 执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。  

    9:slice() – 提取字符串的一部分,并返回一个新字符串。 (参数可以为负数)

    1 var f6="hello world"
    2 console.log(f6.slice(6)) //world
    3 console.log(f6.slice(6,9)) //wor

    10:split() – 方法用于把一个字符串分割成字符串数组。 

    1 var f7="hello world";
    2 console.log(f7.split("")); //["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
    3 console.log(f7.split(" "));  //["hello", "world"]
    4 console.log(f7.split(" ",1)); //["hello"]

    11:toLowerCase() – 将整个字符串转成小写字母。 
    12:toUpperCase() – 将整个字符串转成大写字母。

      

      

  • 相关阅读:
    少走弯路的10条忠告
    思考
    哈弗经典校训
    项目导出excel引发的一些问题
    hibernate 缓存设置
    dubbo简单用法
    sql 类型问题
    spring this.logger.isDebugEnabled()
    红黑树
    归并排序
  • 原文地址:https://www.cnblogs.com/mychz/p/7195029.html
Copyright © 2011-2022 走看看