String 字符串常用的方法

String 字符串常用的方法

 

<body>
    <script>
      //1. split(用于把字符串分割为数组) 把字符串 转换为 数组  和 join() 相反
      let str = "pink$red$blue";
      // str.split ('分隔符')根据某个符号进行分割
      const res = str.split("$");
      console.log(res); //分割的结果为 ['pink', 'red', 'blue']

      // 2.substring 用于字符串的截取(需要截取的第一个字符串的位置  用下标表示,结束的位置(不包含结束的位置)
      let str1 = "今天你做饭了吗";
      console.log(str1.substring(4, 6)); //做饭

      // 3.includes() 判断某个字符串是否包含在另一个字符串中 如果包含返回true 否则返回false
      let str2 = "今天是疯狂星期四吗";
      console.log(str2.includes("疯狂")); //true
      console.log(str2.includes("haha")); //false

      // 4.startsWith()用于检测是否以某个字符开头 如果是以某个字符开头返回为true 否则返回为false
      let str3 = "今天是疯狂星期四吗";
      console.log(str3.startsWith("疯狂")); //false
      console.log(str3.startsWith("今天")); //true

      // 拓展: 字符串是有下标的
      let str4 = "jiangyumeihahah";
      console.log(str4[0]); //j
    </script>
  </body>

 

posted @ 2022-11-24 23:25  噢噢噢J  阅读(17)  评论(0编辑  收藏  举报