1.trim()
去除字符串两端的空白符号
let str = " 你好 ";
console.log(str.trim()); //你好
console.log(str.trim().length); //2
2.toUpperCase()
把字符串里面的小写变大写并返回
const str = "happy day";
console.log(str.toUpperCase()); //HAPPY DAY
3.toLowerCase()
把字符串里面的大写变小写并返回
let str = "HAPPY DAY";
str = str.toLowerCase();
console.log(str); //happy day
4.concat()
将一个或者多个字符串拼与原来的字符串拼接,然后返回一个新的字符串。参数强制转换为字符串然后拼接。
let str = "HELLO WORLD";
str = str.concat("hi", ["nihao"]);
console.log(str); //HELLO WORLDhinihao
5.replace()
在字符串里面插入或者替换一个值
let str = "hello world";
// str = str.replace("world", "sky");
// 结果 hello sky
// console.log(str);
// str = str.replace("world", "$$"); //插入一个 "$"。
// 结果 hello $
// console.log(str)
// str = str.replace("world", "$`"); //插入当前匹配的子串左边的内容。
// 结果 hello hello
// str = str.replace("world", "`$"); // 插入当前匹配的子串右边的内容。
// 结果 hello `$
// console.log(str)
str = str.replace("world", "$'"); // 插入当前匹配的子串右边的内容。
// 结果 hello
console.log(str)
6、slice()
提取字符串的一部分返回一个新的字符串
let str = "你好我叫王小美";
console.log(str.slice(3)); //叫王小美
console.log(str.slice(3, 5)); //叫王
console.log(str.slice(-3)); //王小美
console.log(str.slice(-7, -4)); //你好我
7、includes
查找字符串中是否包含这个值,成功返回true,失败返回false
let str = "你好我叫王小美";
console.log(str.includes("王小美")); //true
console.log(str.includes("甘雨")); //false
8、split()
分割字符串,变成一个字符串,并返回该数组
let str3 = "你好我叫王小美";
console.log(str3.split("")); // ["你", "好", "叫", "小", "王", "美"]
console.log(str3.split("123 ")); //['你好我叫王小美']
9、indexOf
在字符串中搜索指定子字符串,并返回其第一次出现的位置索引。找不到返回-1
let str = "你好我叫王小美,你好我叫王小美";
console.log(str.indexOf("你")); //0
console.log(str.indexOf(",")); //7
console.log(str.indexOf("找不着")); //-1
console.log(str.indexOf("你", 2)); //8
10、toFixed
使用定点表示法来格式化一个数值。常用于小数取整数
let num = 666668844.665789;
console.log(num.toFixed()); //666668845
console.log(num.toFixed(2)); //666668844.67
let num2 = 2.55;
console.log(num2.toFixed(1)); //2.5
let num3 = 2.56;
console.log(num3.toFixed(1)); //2.6 四舍六入