【JavaScript】Summary of string methods (method up to ES7~)

The purpose is to make a note by myself, so that I can check it later~

1. String

1. check

method one:indexOf()

Find a certain character, if there is one, return the position of the first match, otherwise return -1

let str = 'abccefg';
let res = str.indexOf('c');  // 2

Method Two:charAt()

Returns the character at the nth position

let str = 'abccefg';
let res = str.charAt('2');  //c

Method three:charCodeAt()

Returns the ASCII value of the character at position n

let str = 'abccefg';
let res = str.charCodeAt('2');  // 99

For charAt()the method, if the parameter is not between 0and the string length-1, return 空字符串; and for charCodeAt()the method, return NaNinstead of 0or 空字符串.

Method four:lastIndexOf()

Retrieve a string from back to front

let str = 'abccefg';
let res = str.lastIndexOf('c');  // 3

Method five:search()

Retrieves a specified substring in a string, or a substring that matches a regular expression. Returns -1 if no matching substring is found.

let str = "123";
console.log(str.search("3") != -1 );  // true

Method six:includes()

Returns a boolean indicating whether the argument string was found.

let s = 'Hello world!';
s.includes('o') // true

2. increase

method one:concat()

Used to concatenate one or more strings into a new string

let str = 'hello'
let res = str.concat(' 可以任意添加多个参数',' world')
console.log(str); // hello
console.log(res); // hello 可以任意添加多个参数 world

3. delete

The meaning of deletion here is not to delete the content of the original string, but to create a copy of the string and then operate it.

Method 1: slice()
Method 2: substr()The parameter meaning is somewhat different from the other two
methods. Method 3:substring()

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

4. Other

method one:startsWith() endsWith()

startsWith():returnBoolean value, indicating whether the parameter string is at the head of the original string.
endsWith():returnBoolean value, indicating whether the parameter string is at the end of the original string.

let s = 'Hello world!';

s.startsWith('Hello') // true
s.endsWith('!') // true

All three methods support a second parameter, which indicates where to start the search.

let s = 'Hello world!';

s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true

The above code indicates that when the second parameter is used n, endsWiththe behavior is different from the other two methods. It works on the first ncharacter, while the other two methods work from the first nposition until the end of the string.

Method Two:repeat()

repeatThe method returns a new string, which means repeating the original string n.

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

If the parameter is a decimal, it will be rounded up.

'na'.repeat(2.9) // "nana"

If the repeat parameter is a string, it will be converted to a number first.

'na'.repeat('na') // ""
'na'.repeat('3') // "nanana"

Method 3: padStart(),padEnd()

padStart()For head completion, padEnd()for tail completion.

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

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

padStart()and padEnd()accepts a total of two parameters, the first parameter is the maximum length of string completion, and the second parameter is the string used for completion.

If the length of the original string is equal to or greater than the maximum length, string completion will not take effect and the original string will be returned.

'xxx'.padStart(2, 'ab') // 'xxx'
'xxx'.padEnd(2, 'ab') // 'xxx'

Method four: trimStart(),trimEnd()

trimStart()trimEnd()Eliminate leading spaces and trailing spaces in a string . They all return new strings and do not modify the original string.

const s = '  abc  ';

s.trim() // "abc"
s.trimStart() // "abc  "
s.trimEnd() // "  abc"

Method five:replaceAll()

String's instance methods replace()can only replace the first match.

// replace()只将第一个b替换成了下划线。
'aabbcc'.replace('b', '_')
// 'aa_bcc'

// 如果要替换所有的匹配,不得不使用正则表达式的g修饰符。
'aabbcc'.replace(/b/g, '_')
// 'aa__cc'

'aabbcc'.replaceAll('b', '_')
// 'aa__cc'

To be continued...

Guess you like

Origin blog.csdn.net/Bon_nenul/article/details/128215635