关于javascript:JS字符串常用方法

5次阅读

共计 4301 个字符,预计需要花费 11 分钟才能阅读完成。

字符串的罕用办法

字符串的办法很多,然而罕用的就那么些,还有些都没有这么用过,上面简略介绍下

1.charAt(index): 返回指定下表的值,返回值新的字符串,不影响原来的字符串

let str = 'hello';
let newStr = str.charAt(1);
console.log(str)      // hello
console.log(newStr)   // e

2.charCodeAt(index): 返回指定下表的值的 unicode 值,返回值是 Unicode 码,不影响原来的字符串

let str = 'hello';
let newStr = str.charCodeAt(1);
console.log(str)      // hello
console.log(newStr)   // 101

3.fromCharcode(u1,u2,u3): 这个是 String 本人应用的静态方法,参数是 unicode 码,返回对应的值

let str = String.fromCharCode(72,69,76,76,79);
let str1 = String.fromCharCode(97,98,99)
console.log(str)      // HELLO
console.log(str1)     // abc

4.concat(val1,val2,val3,val4…): 连贯两个或多个字符串,不扭转现有的字符串,返回值是拼接后的字符串

let str = 'hello';
let newStr = str.concat('world','!','.....');
console.log(str)      // hello
console.log(newStr)   // helloworld!.....

5.indexOf(str, [start]): 查找字符串中时候蕴含某个值,找到返回第一个呈现的地位的索引值,找不到返回 -1,start 是查找开始的地位,start 的地位蕴含在查找的范畴中,是可选的,能够不写,默认就从 0 开始查起。

let str = 'hello';
console.log(str.indexOf('el'))      // 1
console.log(str.indexOf('al'))      // -1
console.log(str.indexOf('el',2))    // -1

6.lastIndexOf(str, [start]): lastIndexOf 和 indexOf 一样的应用,只是返回的是最初一次呈现的地位索引值,找不到返回 -1,start 是查找开始的地位,是可选的,能够不写,默认就从 string.length- 1 开始查起, 也就是说从后往前面查的。

let str = 'hello';
console.log(str.lastIndexOf('l'))      // 3
console.log(str.lastIndexOf('c'))      // -1
console.log(str.lastIndexOf('l',2))    // 2

7.replace(regexp|str, replacetext): 用于把字符串中的某些字符串替换成其余字符串,然而只替换一次,也就是说如果字符串中有多个,然而只会替换第一个合乎的值。还有一种是应用正则,是把合乎正则的替换成本人要替换的内容。不会扭转原字符串。

let str = 'hello world';
let newStr1 = str.replace('l','a')
let newStr2 = str.replace(/\s*/g,'')
let newStr3 = str.replace(/^\s*|\s*$/g,'')
console.log(str)        // 'hello world'
console.log(newStr1)    // 'healo world'
console.log(newStr2)    // 'helloworld',这个是去除所有空格
console.log(newStr3)    // 'hello world',这个是去除字符串中间的空格 

8.trim(): 删除字符串两端的空格。

let str = 'hello world';
let newStr1 = str.trim()
console.log(str)        // 'hello world'
console.log(newStr1)    // 'healo world'

9.slice(start, [end]): 截取字符串,返回一个新的字符串,不会扭转原字符串。新的字符串是从 start 地位开始,end 完结,然而不包含 end 的地位

let str = 'hello world';
let newStr1 = str.slice(1)
let newStr2 = str.slice(1,3)
console.log(str)        // 'hello world'
console.log(newStr1)    // 'ello world'
console.log(newStr2)    // 'el'

10.substr(start, [length]): 截取字符串,返回一个新的字符串,不会扭转原字符串。新的字符串是从 start 地位开始,截取 length 长度的字符串,没有的话,默认截取到结尾

let str = 'hello world';
let newStr1 = str.substr(1)
let newStr2 = str.substr(1,3)
console.log(str)        // 'hello world'
console.log(newStr1)    // 'ello world'
console.log(newStr2)    // 'ell'

11.substring(start, [end]): 截取字符串,返回一个新的字符串,不会扭转原字符串。新的字符串是从 start 地位开始,end 完结,然而不包含 end 的地位

let str = 'hello world';
let newStr1 = str.substring(1)
let newStr2 = str.substring(1,3)
console.log(str)        // 'hello world'
console.log(newStr1)    // 'ello world'
console.log(newStr2)    // 'el'

12.split(separator, [limit]): 把字符串依照指定宰割成一个数组,一个参数是指定字符串或者正则,第二个参数是数组的最大长度,选填。

let str = 'helloworld';
let str1 = 'hello,world';
let newStr1 = str.split('')
let newStr2 = str1.split(',')
console.log(str)        // helloworld
console.log(newStr1)    // ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
console.log(newStr2)    // ['hello', 'world']

PS:字符串能够转数组,数组也能够转字符串,办法是 join()
let arr = ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
let str = arr.join("")
console.log(str)

13.toLowerCase(str): 把字符串都变成小写,英文

let str = 'HELLOWORLD';
let newStr1 = str.toLowerCase('')
console.log(str)        // HELLOWORLD
console.log(newStr1)    // helloworld

14.toUpperCase(str): 把字符串都变成大写,英文

let str = 'helloworld';
let newStr1 = str.toUpperCase('')
console.log(str)        // helloworld
console.log(newStr1)    // HELLOWORLD

15.repeat(number): 反复字符串,number 必须是一个正整数

let str = 'helloworld';
let newStr1 = str.repeat(3)
console.log(str)        // helloworld
console.log(newStr1)    // helloworldhelloworldhelloworld

16.endsWith(str): 查看字符串是不是以指定字符串完结的

let str = 'helloworld';
let newStr1 = str.endsWith('ld')
console.log(str)        // helloworld
console.log(newStr1)    // true

17.includes(str): 查看字符串是否蕴含指定的字符串或字符

let str = 'helloworld';
let newStr1 = str.includes('ld')
console.log(str)        // helloworld
console.log(newStr1)    // true

18.match(regexp): 依据正则或者字符串来查找字符串中是否蕴含匹配的项,返回数组或者 null

let str = 'helloworld';
let newStr1 = str.match('ld')
console.log(str)        // helloworld
console.log(newStr1)    // ['ld', index: 8, input: 'helloworldld', groups: undefined]

19.search(regexp): 依据正则或者字符串来查找是否蕴含匹配项,有的话返回索引,没有返回 -1

let str = 'helloworld';
let newStr1 = str.search('ld')
console.log(str)        // helloworld
console.log(newStr1)    // 8

20.localeCompare(str): 判断每个字母的排序,在以后字符串后面返回正值,在前面就是负值,一样的就返回 0

let str = 'helloworld';
let newStr1 = str.localeCompare('ad')
let newStr2 = str.localeCompare('helloworld')
let newStr3 = str.localeCompare('halloworld')
let newStr4 = str.localeCompare('id')
console.log(str)        // helloworld
console.log(newStr1)    // 1
console.log(newStr2)    // 0
console.log(newStr3)    // -1
console.log(newStr4)    // -1
正文完
 0