js基础 字符串的方法整理

在底层字符串是以字符串数组的形式保存的
1.charAt():可以返回字符串中指定位置的字符
根据索引获取指定字符

str = ‘hello lfm’;
var result = str.charAt(0);
console.log(str);
//对原字符串无影响
console.log(result);
//h

2.charCodeAt():

charCodeAt()
//获取指定位置字符的字符编码

3.fromCharCode():

fromCharCode();
//根据字符码获取字符
用法
result = String.fromCharCode(73);
console.log(result);
//‘H'

4.concat():用来连接字符串
5.indexOf():该方法可以检索一个字符串中是否含有指定内容

str = 'hello lfm'
result = str.indexOf('h');
console.log(result);
//0 如果字符串中含有该内容,则会返回第一次出现的索引
//如果没有找到指定的内容,返回-1
str = 'hello hlfm';
result = str.indexOf('h',1);
console.log(result);
//4 第二个参数是指定开始查找的位置

6.lastIndexOf():从后往前找,与上差不多
7.str.slice()
8.str.substring()
7,8用法相同,唯一区别就是8的第二个参数不能取负数
9.split():可以将一个字符串拆分为一个数组

str = 'abc,bcd,def,fgh';
result = str.split(',');
console.log(result);
//abc,bcd,def,fgh
console.log(Array.isArray(result));
//true
console.log(result[0]);
//abc

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值