常用的字符串方法大全
⼀、拼接或截取字符串
1.str.concat()
⽤于将⼀个或多个字符串拼接起来,返回拼接后的新字符串
参数:可以有多个,⽤来拼接到str上的字符串
let str ='hello';
console.log(str.concat(' ','world'))//'hello wor
2.str.slice()
此⽅法⽤来提取⼀个字符串,并返回⼀个新的字符串
参数:1)beginIndex,表⽰从该索引处开始提取字符串的字符(包括),如果为负数则从后开始计算
2)endIndex,表⽰从该索引处结束提取字符串(不包括),如果省略则⼀直提取到字符串末尾,如果为负数从后开始计算
let str ='hello world';
console.log(str.slice(6))//'world'
console.log(str.slice(-5,-3)) //'wo'
3.str.substring()
此⽅法和slice⽅法功能相同都是提取⼀个字符串,并返回提取到的字符串
参数:1)startIndex,表⽰从该索引处开始提取