1、concat() 用于将一个或多个字符串拼接成新的字符串

let stringValue = "hello ";
let result = stringValue.concat("world", "!");
console.log(result);// "hello world !"
console.log(stringValue); // "hello"

该方法不会改变原来的字符串值;
作为代替更多使用加号操作符拼接字符串。

2、slice()、substr()、substring() 从字符串中提取子字符串

let stringValue = "hello world"; 
console.log(stringValue.slice(3));// "lo world"
console.log(stringValue.substring(3)); // "lo world" 
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w" 
console.log(stringValue.substr(3, 7)); // "lo worl"

这三个方法不会改变原来的字符串值;
对这三个方法来说,都可以接受一到二个参数,第一个参数都表示提取开始的位置,对slice和substring第二个参数表示提取结束的位置,对substr第二个参数表示返回字符串的数量;
当传入参数为负时,slice将所有负值参数都当成字符串长度加上负参数值,substr方法将第一个负参数值当成字符串长度加上该值,将第二个负参数转化为0,substring方法将将所有负参数都转化为0。

let stringValue = "hello world"; 
console.log(stringValue.slice(-3)); // "rld"
console.log(stringValue.substring(-3)); // "hello world" 
console.log(stringValue.substr(-3)); // "rld" 
console.log(stringValue.slice(3, -4));// "lo w"
console.log(stringValue.substring(3, -4)); // "hel" 
console.log(stringValue.substr(3, -4));// "" (empty string)

3、indexOf()、lastIndexOf() 在字符串中定位子字符串

let stringValue = "hello world"; 
console.log(stringValue.indexOf("o"));// 4
console.log(stringValue.lastIndexOf("o")); // 7

两者没有找到指定字符串时会返回-1;
两者区别是indexOf从开头开始查找,lastIndexOf从结尾开始查找,都可以接受第二个数据,表示开始搜素的位置,都可以接受第二个参数表示开始搜索的位置。

let stringValue = "hello world"; 
console.log(stringValue.indexOf("o", 6));// 7
console.log(stringValue.lastIndexOf("o", 6)); // 4