JS字符串方法(最全最详细)

1、length属性 表示字符串中字符的数量

let strVal = 'hello word china'

console.log(strVal.length)  // 16

2、concat() 用于将一个或多个字符串拼接成一个新字符串

let strVal = 'hello '
let result = strVal.concat('word china')

console.log(strVal)  // hello
console.log(result)  // hello word china

// 等价于
let result = 'hello'+' word china'
console.log(result)  // hello word china

// 等价于
let strVal = 'hello'
let result =`${strVal} word china`
console.log(result)  // hello word china

3、字符串中提取子字符串的方法:slice(), substr(),substring()

let stringVal = 'hello word'
console.log(stringVal.slice(3)) // lo word
console.log(stringVal.substring(3)) // lo word
console.log(stringVal.substr(3)) // lo word
let stringVal = 'hello word'
console.log(stringVal.slice(3,7)) // lo w
console.log(stringVal.substring(3,7)) // lo w
console.log(stringVal.substr(3,7)) // lo word

小张提示:传入两个参数时,substr的第二参数时返回的字符数。所以返回了lo word

3.1 当某个参数是负值时,这三个方法行为不同
  1. slice方法将所有负值参数都当成字符串长度加上负参数值
  2. substr将第一个负参数值当成字符串长度加上该值,将第二个负参数值转换0
  3. substring方法将所有负参数值转化为0
console.log(stringVal.slice(-3)) // ord  11+ (-3)=8
console.log(stringVal.substring(-3)) // hello word  
console.log(stringVal.substr(-3)) // ord

console.log(stringVal.slice(3,-4)) // lo   11+ (-4)=7
console.log(stringVal.substring(3,-4)) // hel  
console.log(stringVal.substr(3,-4)) // ""

小张提示:在给slice()和substr()传入负参数时,他们的返回结果相同。这是因为-3会被转化8,实际上调用的是slice(8)和substr(8)。而substring()方法返回了整个字符串,因为-3转换为0.

提示2:第二个参数时负数时,slice()方法将第二个参数转换为7,时间上调用的是slice(3,7)。substring()方法会将第二个参数转换为0,相当于调用了substring(3,0)等价于substring(0,3),这是因为这个方法会将较小的参数作为起点,将较大的参数作为终点。substr()方法会将第二个参数转为0.意味着返回的字符串包含0个字符,因此返回空字符串

4、字符串位置方法indexOf(),lastIndexOf()

4.1 两个方法只有一个参数
  1. 这两个方法都是从字符串中搜索传入的字符串,并返回位置(如果没找到,则返回-1)
  2. indexOf从字符串开头开始查找子字符串
  3. lastIndexOf从字符串结尾查找子字符串
let stringVal = 'hello word'
console.log(stringVal.indexOf('o')) // 4
console.log(stringVal.lastIndexOf('o')) // 7
console.log(stringVal.lastIndexOf('a')) // -1
4.2 两个方法接收可选第二个参数,表示开始搜索的位置
  1. indexOf会从这个参数指定的位置开始向字符串末尾搜索,忽略该位置之前的字符
  2. lastIndexOf会从这个参数指定的位置开始向字符串开头搜索,忽略该位置之后直到字符串末尾的字符
let stringVal = 'hello word'
console.log(stringVal.indexOf('o',6)) // 7
console.log(stringVal.lastIndexOf('o',6)) // 4

5、字符串包含方法includes(),startsWith(),endsWith()

5.1、只有一个参数

  1. 这三个方法都会从字符串中搜索传入的字符串,并返回一个表示是否包含的布尔值。
  2. startsWith()检查开始于索引0的匹配项
  3. endsWith()检查开始于索引(string.length-substring.length)的匹配项
  4. includes检查整个字符串
let stringVal="zhangsanlisi"
console.log(stringVal.startsWith('zhang')) // true
console.log(stringVal.startsWith('san')) // false

console.log(stringVal.endsWith('san')) // false
console.log(stringVal.endsWith('si'))  // true

console.log(stringVal.includes('san')) // true
console.log(stringVal.includes('zhang')) // true
console.log(stringVal.includes('si')) // true
console.log(stringVal.includes('wang')) // false

5.2、startsWith()和includes()接收第二个参数,表示搜索的位置

let stringVal="zhangsanlisi"
console.log(stringVal.startsWith('zhang'))  // true
console.log(stringVal.startsWith('zhang',1))  // false

console.log(stringVal.includes('san')) // true
console.log(stringVal.includes('san',6)) // false

小张提示:两个方法会从指定的位置开始搜索,忽略该位置之前的所有字符

5.3、endsWith()接收第二个参数,表示字符串末尾的位置

let stringVal="zhangsanlisi"
console.log(stringVal.endsWith('li')) // false
console.log(stringVal.endsWith('li',10)) // true

6、trim()方法会创建字符串的一个副本,删除前、后所有空格符,再返回结果

let stringVal=' hello word '
let result = stringVal.trim()
console.log(stringVal,stringVal.length) // 12
console.log(result,result.length) // 10

7、repeat()方法接收一个整数参数,表示要将字符串复制多少次,然后返回拼接所有副本后结果

let stringVal = '要'
console.log('我'+stringVal.repeat(5)+'这铁棒有何用!') // 我要要要要要这铁棒有何用!

8、padStart()和padEnd复制字符串

8.1、如果小于指定长度,则在相应一边填充字符直至满足长度条件。第一个参数是长度,第二个参数是可选的填充字符串,默认空格

let stringVal = 'zhang'
console.log(stringVal.padStart(9)) // "    zhang"
console.log(stringVal.padStart(9,'*')) // ****zhang

console.log(stringVal.padEnd(9)) // "zhang    "
console.log(stringVal.padEnd(9,'.')) // zhang....

8.2、可选的第二个参数并不限于一个字符。如果提供了多个字符串,则会将七拼接并截断以匹配指定长度。此外,如果长度小于或等于字符串长度,则会返回原始字符串

let stringVal = 'zhang'
console.log(stringVal.padStart(9,'san')) // "sanszhang"
console.log(stringVal.padStart(2)) // zhang

console.log(stringVal.padEnd(9,'san')) // zhangsans
console.log(stringVal.padEnd(2)) // zhang

9、字符串大小写转换

let stringVal = 'hello world'
console.log(stringVal.toLowerCase()) // hello world
console.log(stringVal.toLowerCase()) // hello world
console.log(stringVal.toUpperCase()) // HELLO WORLD
console.log(stringVal.toLocaleUpperCase()) // HELLO WORLD
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值