JavaScript 字符串的方法

大小写转换

可以使用如下两种方法对字符串进行大小写转换。

let str='Hello'
console.log(str.toLowercase())   // =>'hello'
console.log(str.toUpperCase())   // =>'HELLO'

indexOf()和lastIndexOf()方法

可以使用indexOf()返回字符或子串在字符串中第一次出现的索引位置,lastIndexOf()方法类型,只是从字符串的末尾开始查找。

let str = 'hello,world'
console.log(str.indexOf('o'))  //=>4
console.log(str.lastIndexOf('o')) //=>7
console.log(str.indexOf('wo')) //=>6

includes()方法

可以使用includes()方法确认字符串是否包含某个字符或子串,例如:

let str='hello,world'
console.log(str.includes('hello'))    //=>true

startWith()和endsWith()方法

startWith()方法用于确认字符串是否以某个字符或子串开头,而endsWith()方法用于确认字符串是否以某个字符或子串结尾。例如:

let str='hello,world'
console.log(str.startsWith('hello'))  //=> true
console.log(str.endsWith('world'))    //=>true

去除字符串的首尾空格

有三种方法去除字符串的首尾空格,如下所示:

let str='  hello,world  '  // 首尾各有两个空格
console.log(str.trim())      //=>'hello,world',同时去除首尾空格
console.log(str.trimLeft())  //=>'hello,world  ',只去除左边的空格
console.log(str.trimRight()) //=>'  hello,world',只去除右边的空格

重复字符串

使用repeat()方法对字符串进行重复,例如:

let str='hello'
console.log(str.repeat(3))  //=>'hellohellohello'

padStart()和padEnd()方法

有时候需要保证字符串的长度是固定的,就需要在左右使用字符进行填充。

let str='hello'
console.log(str.padStart(10))  //=> '     hello',在左侧填充默认的5个空格
console.log(str.padEnd(10))    //=>'hello     ',在右侧填充5个空格
console.log(str.padStart(3))   //=>'hello',长度足够,原样返回
console.log(str.padStart(10,',')) // =>',,,,,hello',在左侧使用逗号填充

提取子字符串

要提取子字符串,有三种方法。slice()和substring()需要传入提取开始的位置和结束位置,而substr()需要传入开始位置和提取的字数量。

let str='hello,world'
console.log(str.slice(4,7))  //=>'o,w' ,从索引4位置开始提取,到索引7位置之前结束(左闭右开原则)
console.log(str.slice(4))    //=> 'o,world',从索引4位置开始提取,一直到结束
console.log(str.substr(4,3)) // =>'o,w',从索引4位置开始提取,提取3个字符
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值