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() – 将整个字符串转成大写字母。

      

      

  • 相关阅读:
    AC日记——可能的路径 51nod 1247
    AC日记——[国家集训队2011]旅游(宋方睿) cogs 1867
    近期将要学习的内容(flag)
    Cogs 734. [网络流24题] 方格取数问题(最大闭合子图)
    Cogs 746. [网络流24题] 骑士共存(最大独立集)
    Cogs 729. [网络流24题] 圆桌聚餐
    [网络流24题]飞行员配对方案问题
    Hdu 3549 Flow Problem(最大流)
    Cogs 14. [网络流24题] 搭配飞行员(二分图匹配)
    Cogs 728. [网络流24题] 最小路径覆盖问题
  • 原文地址:https://www.cnblogs.com/mychz/p/7195029.html
Copyright © 2011-2022 走看看