JavaScript基础--字符串方法和ES6新增方法

1、charAt()和charCodeAt()

都是用来查询字符串中特定字符的。不同的是charAt返回的是特定字符而charCodeAt返回特定字符的unciode编码

var str = "hello";
str.charAt(2)  // "e"
str.charCodeAt(2)  // "101"

还可以通过中括号的方式来访问

var str = "hello";
str[2]  // "e"

2、concat()

用来将一个或者多个字符串拼接在一起,返回新的字符串,但是不改变原字符串。

var str1 = "hello";
var str2 = "world";
var str3 = str1.concat(str2)
console.log(str3)  // "helloworld"

3、substr()、substring()和slice()

这三个方法都是用来切割字符串返回新的字符串。都有两个参数。

参数一:切割字符串的开始位置

但是第二个参数slice、substring和substr的含义不同。slice和substring表示最后一个字符后面的位置(不包括这个字符),而substr第二个参数表示返回字符的个数

var str = "hello world";
str.slice(3, 7)  // "lo w"
str.substring(3, 7)  // "lo w"
str.substr(3, 7)  // "lo  worl"

只传递一个负数作为参数,slice和substr相同,用负参数加上字符串长度,相当于从后面开始截取。而substring则将负参数转为0返回字符串

第二个参数为负数,slice会将负参数加上字符串长度后在进行截取,substring则将第二个参数转为0,但是substring会以0位开始截取字符串,而较大的参数作为结束位置,substr也会将负参数转为0但是substr不以0为开始位置截取所以返回空字符串

var str = "hello world";
str.slice(-3)  // "rld" 
str.substring(-3)  // "hello world"
str.substr(-3)  // "rld"
str.substr(3, -4)  // "lo w"
str.substring(3, -4) // "hel"
str.slice(3, -4)  //"" 空字符串

4、indexOf()和lastIndexOf()

这两种方法都是搜索指定子字符串在第一次出现字符串的位置,返回子字符串的位置,如果没有则返回-1。indexOf是从前往后,而lastIndexOf是从后往前

var str = "hello world";
str.indexOf(o)  // 4
str.lastIndexOf(o)  //7

可以传递第二参数表示从哪个位置开始截取。

可以使用indexOf或者lastIndexOf来找所有匹配字符串中子字符串

var str = "odmvklmkdfjvndkjnklsdfklkdlv";
var arr = new Array();
var pos = str.indexOf("m");
while(pos > -1) {
    arr.push(pos);
    pos = str.indexOf("m", pos + 1);
}
console.log(arr)

5、trim()

该方法用来删除前置以及后缀的空格,字符串中间的空格删不掉的,返回新的新字符串不改变原来的字符串

var str = "   hello world   ";
var newStr = str.trim()   
console.log(newStr)  //  "hello world"
console.log(str)   // "   hello world   "

6、toLowerCase()、toLocaleLowerCase()、toUpperCase()和toLcaleUppercase()

用来将字符串转换大小写

7、match()、search()、replace()和split()

match只接受一个参数,要么是一个正则表达式要么是一个RegExp对象

search只接受一个参数,,参数是字符串或者RegExp对象,返回第一个匹配项的索引

replace接受两个参数第一个参数是字符串或者RegExp对象,第二参数是可以是一个字符串或者一个函数

function strReplace(str) {
    return text.replace(/[<>"&]/g, function(match, pos, originalText){
        switch(match) {
            case "<":
                return "&lt";
            case ">":
                return "&gt";
            case "&":
                return "&amp";
            case "\"":
                return "&qout"
        }            
    })
}

split指定分隔符将字符串分割多个子字符串并放在一个数组中,第二个参数使用来指定返回数组的长度

var colorText = "red,green,gray,yellow";
colorText.split(",")   //["red", "green", "gray", "yellow"]
colorText.split(",", 2)  // ["red", "green"]

8、localCompare()

用来比较连个字符串。当两个字符串的一样则返回0

9、fromCharCode()

是将一个或者多个字符编码转为字符串

console.log(String.fromCharCode(104,101,108,108,111))  //"hello"

ES6对字符串的新添增的方法

1、字符的unciode表示法

除了采用\uxxxx表示一个字符(但只是限于\u0000~\uFFFF之间的字符),对于超出ES6采用了大括号包括编码

'\u{7A}' === 'z' // true

2、codePonitAt()

用来解决ES5中不能识别四个字节存储的字符,返回的十进制可以采用toString转为十六进制

3、String.fromCodePonit()

该方法为了解决ES5的String.fromCharCode不能识别32 位的 UTF-16 字符(Unicode 编号大于0xFFFF)的问题。注意,fromCodePoint方法定义在String对象上,而codePointAt方法定义在字符串的实例对象上

4、字符串遍历接口

字符串提供了遍历器接口,可以通过for...of循环遍历

for (let codePoint of 'foo') {
  console.log(codePoint)   // "f" "o"  "o"
}

5、includes()、startsWith()、endsWith()

includes用来确定一个字符串是否包含在另一个字符串中,返回true则表示存在false表示不存在

startsWith()表示参数字符串是否在原字符串的头部。返回true则表示存在false表示不存在

endsWith()表示参数字符串是否在原字符串的尾部。返回true则表示存在false表示不存在

6、repeat()

方法返回一个新字符串,表示将原字符串重复n次。参数如果是小数,会向下取整。参数是负数或者Infinity,会报错。NaN等同于 0。参数是字符串,则会先转换成数字

'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"
'na'.repeat(0) // ""

7、padStart()、padEnd()

padStart()用于头部补全,padEnd()用于尾部补全。第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串

'x'.padStart(4, 'ab') // 'abax'
'x'.padEnd(5, 'ab') // 'xabab'

8、matchAll()

返回一个正则表达式在当前字符串的所有匹配

9、字符串模板

用反引号(`)标识。它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量

let name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`

大括号内部可以放入任意的 JavaScript 表达式,可以进行运算,以及引用对象属性。模板字符串之中还能调用函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值