Make a summary every day, persistence is victory!
/** @date 2021-06-21 @description String */Copy the code
One (sequence)
String is a basic data type in JavaScript that represents a String;
Strings can be declared in two ways:
literal
Statement:
const str = 'string';
Copy the code
- structure
function
The statement
Const strObj = new String(' String '); const strObj = new String(' String '); const str = strObj.toString(); // const str = strObj.valueOf();Copy the code
Ii (Common Methods)
Common code:
const str = 'string';
Copy the code
charAt
: Gets the NTH character
let s = str.charAt(3); Const len = str.length; const len = str.length; const len = str.length; const len = str.length; // 6 s = str[2]; // 'r' // Out of range returns an empty string s = str.charat (-1); // '' s = str.charAt(6); / /"Copy the code
charCodeAt
: Gets the Unicode code for the NTH character
str.charCodeAt(2); // 114 // Out of range returns NaN str.charcodeat (6); // NaNCopy the code
concat
: concatenates the string and returns the concatenated string
const name = 'E1e', age = 18;
const info = 'name: '.concat(name, ',' , 'age: ', age); // 'name: E1e,age: 18'
Copy the code
endsWith
: whether to end in a substring. You can pass in a length to indicate the length of the STR to be operated on. The default is str.length
str.endsWith('ing'); // true
str.endsWith('str'); // false
str.endsWith('str', 3); // true
Copy the code
includes
: Whether the string contains a substring, pass the substring, the index position to start the judgment, default is 0, that is, start the judgment from scratch
str.includes('str'); // true
str.includes('str', 2); // false
Copy the code
indexOf
: Passes in a substring that returns the index of the first occurrence of the substring. You can pass in the starting position of the search, 0 by default, or -1 if no substring is found
STR. IndexOf (' ing '); // 3 str.indexof ('ing', 4); // -1 str.indexOf(''); / / 0Copy the code
lastIndexOf
And:indexOf
Similar, but returns the last location seen, so look backwards
const repeatStr = 'stringstring'; repeatStr.lastIndexOf('i'); // 9 repeatStr.lastIndexOf('i', 6); // 3 repeatStr.lastIndexOf(''); / / 12Copy the code
match
: Passes in a re and returns an array of all the substrings in the string that satisfy that re
repeatStr.match(/i/g); // ['i', 'i']Copy the code
matchAll
: Passes in a re that returns iterators for all substrings in the string that satisfy this re
[...repeatStr.matchAll(/s/g)]; // [['s'], ['s']]
const iterator = repeatStr.matchAll(/s/g);
iterator.next(); // {value: ['i'], done: false}
iterator.next(); // {value: ['i'], done: false}
iterator.next(); // {value: undefined, done: true}
Copy the code
padEnd
: starts at the tail with the character passed in (default is' '
) fills the string until the string length reaches the passed length, and returns the filled string
str.padEnd(10, '.'); // 'string.... ' str.padEnd(10); // 'string 'Copy the code
11. PadStart: Fills the string again from scratch
str.padStart(10, '.'); / / '... string' str.padStart(10); // ' string'Copy the code
repeat
: Constructs a repeated string based on the number of repetitions passed in and returns it
str.repeat(2); // stringstring str.repeat(3); // stringstringstring str.repeat(); / /"Copy the code
replace
: replaces the first argument in the string with the second argument, which can beString or re
, the second argument can beString or function
(Return string)
str.replace('ing', 'str'); // 'strstr'
str.replace(/[a-z]/g, '1'); // '111111'
str.replace(/[a-z]/g, () => 's'); // 'ssssss'
Copy the code
replaceAll
And:replace
The difference is,replaceAll
Will replaceall
Matched value
RepeatStr. ReplaceAll (' ing ', 'STR'); / / 'STRSTRSTRSTR'Copy the code
search
: pass in aregular
Expression that returns the first match in a stringThe index
, returns if no match is found- 1
RepeatStr. Search (/ I/g); / / 3 repeatStr. Search (/ q/g); / / 1Copy the code
slice
: according to thestartIndex
andendIndex
Extract string, returns extracted string, does not change the original string,startIndex
The default is0
.endIndex
Default is stringThe length of the
str.slice(0, 3); // 'str'
str.slice(); // 'string'
Copy the code
split
: separates the string by delimitersegmentation
, returns aAn array of
, the second parameter indicatesThe length of the array
str.split(); // ["string"]
str.split('i'); ["str", "ng"]
str.split('i', 1); ["str"]
Copy the code
startsWith
And:endsWith
Similar, butFrom the beginning
Begin to match
str.startsWith('str'); // true
str.startsWith('ing'); // false
str.startsWith('ing', 3); // true
Copy the code
substring
And:slice
similar
str.substring(0, 3); // 'str'
str.substring(); // 'string'
Copy the code
toLowerCase
: converts the string tolowercase
In the form of
'STRING'.toLowerCase(); // 'string'
Copy the code
toUpperCase
: converts the string toA capital
In the form of
'string'.toUpperCase(); // 'STRING'
Copy the code
trim, trimStart, trimEnd
: Removes charactersLeft/right/beginning/end
Whitespace character
const spaceStr = ' string ';
spaceStr.trim(); // 'string'
spaceStr.trimStart(); 'string '
spaceStr.trimStart(); ' string'
Copy the code