字符串的一些基本用法

127 阅读1分钟

1.String.charAt():返回字符串中指定下标的字符

let str = "helloWard";
str.charAt(1);  // e

2.String.concat():连接两个或更多字符串

let str = "hello";
let str1 = "Ward"
str.concat(str1)    // helloWard

3.String.includes():寻找字符串中是否有符合条件的字符

let str = "helloWard";
str.includes("h")   // true

4.String.replace():替换字符串中的某个部分

let str = "helloWard";
str.replace("he","aa")  // aalloWard

5.String.slice():提取字符串中的某个部分,并且返回新的字符串

let str = "helloWard";
str.slice(1,3)  // el

6.String.split():把字符串切割成字符串数组

let str = "1|2|3|12|5";
str.split("|")  // ["1", "2", "3", "12", "5"]