面试官:Javscript字符串的常用方法有哪些?

字符串的方法总的来说就是增删改查

增:并不俗hi直接添加内容,而是创建字符串的一个副本,在进行操作,出来常用的+以及{}进行字符串拼接之外,还可以通过concat

  • concat:用于将一个或者多个字符串拼接成一个新字符串
    let string = 'nihao'
    let result = string.concat('zhangsan')
    console.log(result) // 'nihaozhangsan'
    console.log(string) // 'nihao'

删:不是说删除原字符换的内容而是创建一个字符串的副本,在进行操作

  • slice()
  • substr()
  • substring()
    let string = "nihaozhangsan"
    console.log(string.slice(3)) // 'aozhangsan'
    console.log(string.substring(3)) // 'aozhangsan'
    console.log(string.substr(3)) // 'aozhangsan'
    console.log(string.slice(3, 7)) // 'aoz'
    console.log(string.substring(3, 7)) // 'aoz'
    console.log(string.substr(3, 7))  // 'aozhangs'

改:同上

  • trim()、trimLeft()、trimRight()
  • repeat()
  • padStart()、padEnd()
  • toLowerCase()、toUpperCase()

 trim()、trimLeft()、trimRight(): 删除前、后或前后所有空格符,再返回新的字符串

let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"

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

let stringValue = "na ";
let copyResult = stringValue.repeat(2) // na na 

padEnd(): 复制字符串,如果小于指定长度,则在相应一边填充字符,直至满足长度条件

let stringValue = "foo";
console.log(stringValue.padStart(6)); // " foo"
console.log(stringValue.padStart(9, ".")); // "......foo"

toLowerCase()、toUpperCase():字符串大小写转化

let stringValue = "hello world";
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLowerCase()); // "hello world"

查:通过索引的厂房获取字符串的值

  • chatAt()
  • indexOf()
  • startWith()
  • includes()
     

charAt():返回给定索引位置的字符,由传给方法的整数参数指定

let message = "abcde";
console.log(message.charAt(2)); // "c"

 indexOf:从字符串开头去搜索传入的字符串,并返回位置(如果没找到就会返回-1)

let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4

startWith()、includes():从字符串中搜索传入的字符串,并返回一个表示是否包含的布尔值

let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("bar")); // false
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false

二、转化

split:把字符串按照指定的分隔符,拆分成数组中的每一项

let str = '12+34+56'
console.log(str.split('+')) // [12, 34, 56]

三、模板匹配方法

  • match()
  • search()
  • replace()

 match:接受一个参数可以是一个正则表达式字符串,也可以是一个RegExp对象,返回一个数组

let text = 'cat, bat, sat, fat'
let pattern = /.at/
let matches = text.match(pattern)
console.log(matches[0]) //'cat'

search:接受一个参数,可以是增长表达式字符串,可以是一个RegExp对象,找到则返回匹配索引,否则返回 -1

let text = "cat, bat, sat, fat";
let pos = text.search(/at/);
console.log(pos); // 1

replace: 接收两个参数,第一个参数为匹配的内容,第二个参数为替换的元素(可用函数)

let text = "cat, bat, sat, fat";
let result = text.replace("at", "ond");
console.log(result); // "cond, bat, sat, fat"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值