字符串的属性

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 四舍六入

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值