JavaScript中常用的字符串函数

JavaScript中常用的字符串函数

整理一下js中的字符串函数

1.chaAt()

获取某个位置上的字符
var str = "hello world, it's a nice day";
console.log(str.charAt(0));//返回h

2.charCodeAt()

获取某个位置上的字符的编码
var str = "hello world, it's a nice day";
console.log(str.charCodeAt(0));//返回104(l的编码)

3.indexOf()

从前向后查找某个字符,返回这个字符第一次出现的位置。如果没有,返回-1
var str = "hello world, it's a nice day";
console.log(str.indexOf('l'));返回2

4.lastIndexOf()

从后向前查找某个字符,返回这个字符第一次出现的位置。如果没有,返回-1
var str = "hello world, it's a nice day";
console.log(str.lastIndexOf('l'));//返回9

5.concat()

拼接字符串,将传递的参数拼接到字符串中,不改变原字符串
var str = "hello world";
console.log(str.concat(",it's a nice day"));//返回hello world,it's a nice day

6.slice()

截取指定位置字符串,参数为开始位置和结束位置,不包括结束位置,不改变原字符串
var str = "hello world";
console.log(str.slice(0, 5));//返回hello

7.substring()

和slice()的作用一样
var str = "hello world";
console.log(str.substring(0, 5));//返回hello

8.substr()

截取字符串,参数为开始位置和截取的个数,不改变原字符串
var str = "hello world";
console.log(str.substr(6, 5));

9.trim()

去除前后空格,不改变原字符串
var str = "   hello world      ";
console.log(str.trim());//返回hello world

10.toLowerCase()

将字符串转为小写,不改变原字符串
var str = "HELLO WORLD";
console.log(str.toLowerCase());//返回hello world

11.toUpperCase()

将字符串转为大写,不改变原字符串
var str = "hello world";
console.log(str.toUpperCase());//返回HELLO WORLD
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值