字符串常用API

1.toLowerCase() toUpperCase() 转换大小写,返回新的字符串

 var str1 = "Hello World";
      console.log(str1.toLowerCase());
      console.log(str1.toUpperCase());

2.charAt(index) 根据索引获取字符串中的某一个字符

 console.log(str1.charAt(1));

3.charCodeAt(index)根据索引获取字符串中的某一个字符的ASCII编码

console.log(str1.charCodeAt(1));

4.startsWith() endsWith() 判断一个字符串是否以某个字符或字符串开头/结尾,返回布尔值

console.log(str1.startsWith("Hello")); 
      console.log(str1.endsWith("ld")); 

5.includes(str,stratIndex) 判断一个字符串中是否包含某个字符串,返回布尔值

 console.log(str1.includes("Hello")); 
      var str2 = "hello,你好,hello";
      console.log(str2.includes("hello", 10));

6.trim() 去除字符串两端的空格

 var str3 = "   你  好    ";
      console.log(str3);
      console.log(str3.trim());

7.replace(oldstr,newstr) 替换字符串中的某一部分,返回新的字符串

console.log(str1.replace("World", "世界"));

8.split(’/’) 以某一特殊字符,将字符串分割为数组。

   var str4 = "苹果/香蕉/橘子";
      console.log(str4.split("/"));

9.substr(startindex,lenght) 截取字符串

   var str5 = "aabhellohcchdd";
      console.log(str5.substr(3, 5)); 

10.indexOf() 查询指定字符在字符串中第一次出现的索引

 console.log(str5.indexOf("h")); //5  查询h在str5中第一次出现的索引号
      console.log(str5.indexOf("h", 5)); //8  从指定位置查询 h 在 str5中第一次出现的索引号。
      // lastIndexOf()(反向查找)查询指定字符在字符串中最后一次出现的位置
      console.log(str5.lastIndexOf("h", 10)); 
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值