字符串的常见方法包括操作方法,转换方法,匹配方法
1.操作方法
-
concat
用于将一个或多个字符串拼接成一个新字符串
let string1 = "hello" let string2 = string1.concat("world") console.log(string2) // "helloworld" console.log(string1) // "hello"
除了上述方法之外,还可以使用+
或者模板字符串使用${}
进行拼接
let string1 = "hello"
string1 = string1 + "world"
console.log(string1) // "helloworld"
let string2 = "world"
let result = `${string1}${string2}`
console.log(result) // "helloworld"
slice()
、 substr()
、 substring()
三个方法都是返回调用它们的字符串的一个子字符串,
上述三个方法类似,需要注意的是substr()
第二个参数是需要提取的长度
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo worl