字符串的方法使用总结

一.拼接或截取字符串

str.concat():用于将一个或多个字符串拼接起来,返回拼接后的新字符串

语法1;str. concat( )
参数:可以有多个,用来拼接到str上的字符串

let str = 'hello';
console.log(str.concat(' ','world')) //'hello world'
//此方法效率并不高,用“+”或者``模板字符串代替比较好

str.slice():此方法用来提取一个字符串,并返回一个新的字符串

语法:str.slice(start,end)
参数:1)start,表示从该索引处开始提取字符串的字符(包括),如果为负数则从后开始计算
​ 2)end,表示从该索引处结束提取字符串(不包括),如果省略则一直提取到字符串末尾,如果为负数从后开始计算
let str = 'aisiwangbeiyue';
console.log(str.slice(6)) //ngbeiyue
console.log(str.slice(-5,-3)) //ei

3.

str.substring():此方法和slice方法功能相同都是提取一个字符串,并返回提取到的字符串

语法:str.substring(indexStart[, indexEnd])
参数:1)indexStart,表示从该索引处开始提取字符串的字符(包括)
​ 2) indexEnd,表示从该索引处结束提取字符串(不包括)
​ 3)上述两个参数:如果为负数或者NaN则都会被当做0,如果大于字符串的长度则会被当做字符串的长度来计算,如果 startIndex 大于 endIndex,则 substring 的执行效果就像两个参数调换了一样
let str = 'hello world';
console.log(str.substring(-1,5))  //'hello'
console.log(str.substring(5,-1))  //'hello'

4.

substr()方法从起始索引号提取字符串中指定数目的字符,就是从那开始截,截几个,参数为索引和length,返回值是截取的字符串。,截取完成之后对原字符串无影响。

let str = "abcdef";
   console.log(str.substr(0, 5));//abcde
   console.log(str.substr(4));//ef--写一个参数为索引,默认截取字符串该索引后面的所有字符
字符串去空

str.trim():删除一个字符串两端的空白字符,并返回删除后的新字符串,不会改变原有字符串

   let str = "  wangbeiyue xihuan ni ";
   console.log(str.length);//23
   console.log(str.trim().length);//20
   console.log(str.length);//23
   console.log(str.replace(/\s/g, ''));//wangbeiyuexihuanni

元素替换方法

str.replace():可以将一个替换值替换字符串的一部分,返回替换后的新字符串

  • 替换指定字符对原字符串无影响

let str = 'hello world';
console.log(str.replace(/o/g,"f")) //"hellf wfrld"
字符串转化为数组

str.split():可以使用一个指定的分隔符来将字符串拆分成数组,返回一个数组

参数:1)分隔符,可以为一个字符串也可以为正则表达式,为空的话则将每个字符都拆分。默认全局拆分
2)拆分的长度(可选),为一个整数用来限制拆分的个数,如果超过了这个数量则新数组中不返回剩下的文本
let str = 'hello world';
console.log(str.split(" ")) //["hello", "world"]

查询字符串

1.

str.charAt():从一个字符串中返回指定的字符

参数:index,介于0~length-1之间的整数,默认为0
let str = 'hello world';
console.log(str.charAt(1)) //'e'

2.

str.includes():判断字符串中是否包含指定字符,包含则返回true,不包含则返回false

语法:str.includes(subStr)
参数:subStr,指定的字符串
let str = 'hello world';
console.log(str.includes('hello')) //true
console.log(str.includes('fols')) //flase

3.

str.indexOf():判断字符串中是否包含指定字符,如果包含则返回该字符索引的位置(查找到了立即返回),如果不包含则返回-1。str.lastIndexOf()的用法和indexOf基本相同,区别是lastIndexOf()是从后往前查找

语法:str.indexOf(subStr)
参数:subStr,指定的字符串
let str = 'hello world';
console.log(str.indexOf('world')) //6
console.log(str.indexOf('fols')) //-1

4.

str.search():使用正则表达式查找指定字符串,如果找到则返回首次匹配成功的索引,没有找到则返回-1

参数:一个正则表达式,如果传入一个非正则表达式则会隐式的将其转换为正则表达式对象
let str = 'hello world';
console.log(str.search('world')) //6
console.log(str.search(/aa/)) //-1

5.

str.match():返回一个字符串匹配正则表达式的结果,如果未设置全局匹配,则会返回第一个完整匹配及其相关的捕获组,捕获组中包含有groups、index、input等属性。

参数:一个正则表达式,如果传入一个非正则表达式则会隐式的将其转换为正则表达式对象
let str = 'hello world';
console.log(str.match(/l/)) //["l", index: 2, input: "hello world", groups: undefined]
console.log(str.match(/l/g)) //["l", "l", "l"]
大小写转换

(1)toLocaleLowerCase() --转化为本地格式的小写

(2)toLocaleUpperCase() --转化为本地格式的大写

(3)toLowerCase()-----------把字符串转换为小写

(4)toUpperCase()-----------把字符串转换为大写

  let str = "abc";
   console.log(str.toLocaleLowerCase());//abc
   console.log(str.toLocaleUpperCase());//ABC
   console.log(str.toLowerCase());//abc
   console.log(str.toUpperCase());//ABC

数字精确到小数点

toFiced()方法:数字精确到小数点

<script>var num =99;   console.log(num.toFixed(2));//99.00--保留2位小数点</script
返回某个字符串对象的原始值

valueOf()方法:返回某个字符串对象的原始值

var k1 = "happy";
   console.log(k1.valueOf());  //happy  //string
   var k2 = new String("book");
   console.log(k2);//String ('book')
   console.log(k2.valueOf());//book  //object
   console.log(k2.toString());//book

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值