js复习笔记(二): 字符串方法

· 字符串

创建

字符串一般直接创建:

let str = 'abvkl' 

也可以通过new来创建(不推荐):

let str = new String("abv")

因为通过new创建的是对象,性能不好.而且相同的字符串不能用===来判断,因为它们是不同的对象.

字符串方法和属性

注意: 字符串的方法都是会返回新的字符串,并不会改变原数据!!!

1.查找

length 返回字符串长度
indexOf() 返回指定字符串在文本中首次出现的索引, 如果没有则返回-1 范围: 第二个参数到最后
lastIndexOf() 返回的是最后一次出现的索引,如果没有返回-1 范围: 0到第二个参数
includes() 指定字符串是否包含在某个字符串里,返回true/false(indexOf()返回的是索引值/-1)
startsWith() 是否以某个字符开头,第二个参数表示开始位置,省略则默认为0
endsWith() 是否以某个字符结束,第二个参数表示结束位置,省略则默认最后一位

            let str = 'this is a test'
            console.log(str.indexOf('is')) // 2
            console.log(str.lastIndexOf('is')) // 5
            console.log(str.indexOf('is', 11)) // -1(因为是从第11位开始找的,所以没找到,返回-1)
            console.log(str.lastIndexOf('is', 11)) // 5(0到11之间找)
            console.log(str.includes('q')) // false
            console.log(str.startsWith('a', 2)) // false
            console.log(str.startsWith('i', 2)) // true
            console.log(str.endsWith('i', 4)) // false
            console.log(str.endsWith('i', 3)) // true

search() 搜索特定字符串,返回位置
区别:
search()可以用正则表达式,indexOf()不能;
search()不能设置第二个参数,indexOf()可以.
match() 返回字符串或者正则表达式的匹配
matchAll() 返回一个正则表达式在当前字符串的所有匹配

            let str1 = "1 plus 1 equals 2"
            console.log(str1.match("s")) // ["s", index: 5, input: "1 plus 1 equals 2", groups: undefined]
            console.log(str1.match(/\d+/g)) //  ["1", "1", "2"]
            console.log([...str1.matchAll(/\d+/g)]) //  [Array(1), Array(1), Array(1)](具体见下图)
[...str1.matchAll(/\d+/g)]
2.截取

slice(start, end) 返回start到end的部分(包括start,不包括end)
如果参数是负数,则从倒数开始
如果只有一个参数,则截取从这个参数到末尾

            let str = 'this is a test'
            console.log(str.slice(5, 7)) // is 
            console.log(str.slice(-5, -8)) //  
            console.log(str.slice(-8, -5)) // s a
            console.log(str.slice(5)) // is a test
            console.log(str.slice(-4)) // test

substring(start, end) 和slice类似,参数不能接受负值

            let str = 'this is a test'
            console.log(str.substring(5, 7)) // is
            console.log(str.substring(5)) // is a test

substr(start, length) 第一个参数表示开始位置,第二个参数是截取长度
第一个参数可以为负
如果省略第二个参数则到结尾

            let str = 'this is a test'
            console.log(str.substr(5, 7)) // is a te
            console.log(str.substr(5)) // is a test
            console.log(str.substr(-5)) //  test
3.字符串去空格

trim() 去掉两头空格
trimStart()/trimLeft() 去掉左边空格
trimEnd()/trimRight() 去掉右边空格

            let mystr = "   1234a  "
            console.log(mystr.trim()) // 1234a
            console.log(mystr.trimLeft()) // 1234a  
            console.log(mystr.trimStart()) // 1234a  
            console.log(mystr.trimRight()) //    1234a
            console.log(mystr.trimEnd()) //    1234a
4.字符串替换

replace() 用第二个参数替换第一个参数(只替换找到的第一个)
可以用正则表达式
正则表达式用/XXX/,不是引号
正则的i表示不区分大小写,g表示全局查找(会替换所有的)

            let str = 'this is Is a test'
            console.log(str.replace("is", "at")) // that is Is a test
            console.log(str.replace(/is/i, "at")) // that is Is a test
            console.log(str.replace(/is/g, "at")) // that at Is a test
            console.log(str.replace(/is/gi, "at")) // that at at a test
5.提取字符串字符

charAt() 返回指定下标的字符
charCodeAt() 返回指定下标字符的unicode 编码
也可以用属性访问(不推荐): str[index]

            let str = 'this is Is a test'
            console.log(str.charAt(1)) // h
            console.log(str.charCodeAt(1)) // 104
            console.log(str[1]) // h
6.连接

concat() 连接两个字符串
相当于+

            let str = 'this is Is a test'
            console.log(str.concat("Wedesday", "wrong")) // this is a testWedesdaywrong
            console.log(str + "Wedesday" + "wrong") // this is a testWedesdaywrong

concat()方法也不会改变原字符串!

7.大小写转换

toUpperCase() 转为大写
toLowerCase 转为小写

8.字符串转为数组

split() 将字符串切割成数组

            let str2 = "hello"
            console.log(str2.split()) // ["hello"]
            console.log(str2.split("")) // ["h", "e", "l", "l", "o"]
9.字符串重复

repeat() 表示重复几次
(参数如果是小数,会被取整,如果是0到-1之间的小数,会被视为0,是其他负数则报错,如果是字符串,则会先被转成数字类型)

const str="safijjkc";
console.log(str.repeat(3))//safijjkcsafijjkcsafijjkc
10.长度补全

padSrart() padEnd() 如果长度不够,则会在头部或者尾部自动补全(第一个参数表示位数)
省略第二个参数,则会用空格补全

            console.log('ww'.padStart(5,'ab'));//abaww
            console.log('ww'.padEnd(5,'ab'));//wwaba
            console.log('ww'.padStart(5));//   ww

你可能感兴趣的:(js复习笔记(二): 字符串方法)