js字符串方法

js字符串方法

toUpperCase()

返回转为大写形式的字符串

 let str = "hello"
 console.log(str.toUpperCase());//HELLO

toLowerCase

返回转为小写形式的字符串

let str = "HELLO"
console.log(str.toLocaleLowerCase());//hello

concat

拼接字符串

 let str = "hello";
 let res = str.concat("s", ["d"]); // 拼接成数组
 console.log(res);//hellosd

slice()

slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。

    let str = "hello";
    let res = str.slice(1, 4);
    console.log(res);//ell

includes()

includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。

    let str = "hello";
    console.log(str.includes("ell"));//true
    console.log(str.includes("wo"));//false

startsWith()

startsWith() 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false。

    let str = "hello";
    console.log(str.startsWith("he"));//true
    console.log(str.startsWith("e"));//false

endsWith()

endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。

    let str = "hello";
    console.log(str.endsWith("he"));//false
    console.log(str.endsWith("lo"));//true

split()

split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。不会改变原字符串

    let str = "hello";
    console.log(str.split(""));//(6) ['h', 'e', 'l', 'l', 'o']
    console.log(str.split("l"));//(3) ['he', '', 'o']

indexOf()

indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。字母区分大小写

    let str = "hello";
    console.log(str.indexOf("e"));//1
    console.log(str.indexOf("o"));//4
    console.log(str.indexOf("p"));//-1

toFixed()

toFixed() 可把 Number 四舍五入为指定小数位数的数字

   let num = 1232144.5154
   console.log(num.toFixed());//四舍五入   1232145
   console.log(num.toFixed(2));//四舍五入  1232144.52

toFixed()的使用误差(四舍六入)解决方法
链接: 解决方法

解构

解构(Destructuring)是一种允许我们将数组或对象的属性分解赋值给单独的变量的JavaScript语法。这样可以更直观、更方便地从数组或对象中提取值

   let obj = {
     name: "张三",
     sex: "男",
     age: "18"
   }
   //没有解构
   console.log(obj.name);//张三
   console.log(obj.sex);//男
   console.log(obj.age);//18
   //运用解构
   const { name, sex, age } = obj
   console.log(name, sex, age);//张三 男 18

展开运算符

    let arr = [1, 2, 3, 4, 5]
    let arr2 = [6, 7, 8, 9, 0]
    let arr3 = arr.concat(arr2)
    console.log(arr3);//(10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    console.log([...arr, ...arr2]);//(10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

setTimeout 延时器

用来指定某个函数或字符串在指定的毫秒数之后执行

    setTimeout(function () {
      console.log('执行一次')
    }, 3000)
    setTimeout(() => {
      console.log('执行一次')
    }, 4000)

setInterval 定时器

用来指定某个函数或字符串在指定的间隔内不停的执行

   setInterval(() => {
      console.log('反复执行')
    }, 1000)

clearInterval 清除定时器

清除定时器

      const timerId = setInterval(() => {
        console.log(123);
      }, 500);

      console.log(timerId);

      setTimeout(()=>{
        clearInterval(timerId)
      },5000)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值