1. 程式人生 > >String常用方法

String常用方法

exp 常用 char -h 查看 school cond 分享 根據

1.length 查看字符串的長度

技術分享

2.indexOf()查看對應字符的索引值

技術分享

3.lastIndexOf() 從後面開始查找,返回索引值

技術分享

4.search() 查看包含字符的索引值

技術分享

eq:

  • The search() method cannot take a second start position argument.
  • The search() method can take much more powerful search values (regular expressions).

5.slice() 切割字符串

技術分享

5.substring() 返回字符串片段 效果和slice幾乎一樣,但是不允許索引值為負數

6.substr()返回切割字符串的部分

技術分享

7.replace() 代替字符串,可使用正則表達式

技術分享

8.concat() 合並字符串,可以同時合並多個字符串

技術分享

9.charAt()根據索引值找到對應的字符

技術分享

10.charCodeAt() 返回對應字符的Unicode編碼

技術分享

11.split() 將字符串轉為數組

技術分享

完整的方法參考:https://www.w3schools.com/jsref/jsref_obj_string.asp

String常用方法