不改变原字符串
增--concat()
let stringValue = "hello";
let result = stringValue.concat("world");
console.log(result); // "helloworld"
console.log(stringValue); // "hello"
删--slice()、subString()、substr()
let stringValue = "hello world";
//返回开始-结束(可省,默认到末尾)
console.log(stringValue.slice(2)); // "llo world" (3-末尾)
console.log(stringValue.slice(2, 7)); // "llo w"(3-6)
//返回开始-结束(可省,默认到末尾)
console.log(stringValue.substring(2)); // "llo world" (3-末尾)
console.log(stringValue.substring(2,7)); // "llo w" (3-6)
// substr()
console.log(stringValue.substr(2)); // "llo world" (3-末尾)
console.log(stringValue.substr(2, 7)); // "llo worl" (从3开始的,7个元素)
console.log(stringValue); //hello world
改
trim() 、trimLeft()、trimRright()删除前后的所有空格符
let stringValue = " hello world ";
let trimStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimStringValue); // "hello world"
repeat() 接收一个整数,表示复制次数
let stringValue = "hello ";
let copyStringValue = stringValue.repeat(2) // hello hello
console.log(copyStringValue); //hello hello
console.log(stringValue); //hello
toLowerCase()、 toUpperCase() 大小写转换
let stringValue = "hello world";
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLowerCase()); // "hello world"
查
charAt() 返回给定索引位置的字符
let message = "hello";
console.log(message.charAt(1)); // "e"
indexOf() 返回字符第一位置,否则返回-1
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
startWith()、includes() 返回true or false
startWith() 只能从开头开始匹配
let message = "hello world";
console.log(message.startsWith("hel")); // true
console.log(message.startsWith("llo")); // false
console.log(message.startsWith("lle")); // false
console.log(message.includes("llo")); // true
console.log(message.includes("le")); // false