Blogger Information
Blog 94
fans 0
comment 0
visits 92683
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】JS 之字符串常用 API:(查找,包含,拼装,替换,转换数组)
可乐随笔
Original
370 people have browsed it

字符串常用 API

  1. let str = 'PHP中文网'

1. length属性

  1. console.log('length = ', str.length)

2. charAt(), 索引 -> 成员

  1. console.log(str.charAt(3))
  2. console.log(str[3])

3. indexOf() ,成员 -> 索引

  1. console.log(str.indexOf('中'))

4. includes() 是否包含,返回true/false

  1. console.log(str.includes('中'))

5. concat() 拼装,更多用 + 或 ``

  1. console.log(str.concat('[ ', 'www.php.cn', ' ]'))
  2. //输出:PHP中文网[ www.php.cn ]

6. replace() 替换

  1. console.log(str.replace('中文网','.cn'))
  2. //将‘中文网’ 替换成 ‘.cn’

7. substring(), 必须去掉(忽略)结束索引,就是索引要多写一位

  1. console.log(str.substr(0,3))
  2. console.log(str.substring(0,3))
  3. //substr淘汰,建议使用substring()
  4. console.log(str.substr(-3,3))
  5. //反向取3个

8. split 字符串转数组

  1. console.log(str.split())
  2. //输出:[ 'PHP中文网' ]
  3. console.log(str.split(''))
  4. //输出:[ 'P', 'H', 'P', '中', '文', '网' ]
  5. console.log(str.split('',3))
  6. //输出:[ 'P', 'H', 'P' ]

9.大小写转化

  1. console.log(str.toLocaleLowerCase())
  2. console.log('PHP.CN'.toLocaleLowerCase())
  3. console.log('php.cn'.toLocaleUpperCase())

10.html

  1. // 成生a标签
  2. console.log(str.link('https://www.php.cn'))
  3. //str:链接的文本,link值为a.href
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post