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:

  1. literalStatement:
const str = 'string';
Copy the code
  1. structurefunctionThe 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
  1. 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
  1. charCodeAt: Gets the Unicode code for the NTH character
str.charCodeAt(2); // 114 // Out of range returns NaN str.charcodeat (6); // NaNCopy the code
  1. 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
  1. 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
  1. 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
  1. 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
  1. lastIndexOfAnd:indexOfSimilar, 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. replaceAllAnd:replaceThe difference is,replaceAllWill replaceallMatched value
RepeatStr. ReplaceAll (' ing ', 'STR'); / / 'STRSTRSTRSTR'Copy the code
  1. search: pass in aregularExpression 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
  1. slice: according to thestartIndexandendIndexExtract string, returns extracted string, does not change the original string,startIndexThe default is0.endIndexDefault is stringThe length of the
str.slice(0, 3); // 'str'
str.slice(); // 'string'
Copy the code
  1. 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
  1. startsWithAnd:endsWithSimilar, butFrom the beginningBegin to match
str.startsWith('str'); // true
str.startsWith('ing'); // false
str.startsWith('ing', 3); // true
Copy the code
  1. substringAnd:slicesimilar
str.substring(0, 3); // 'str'
str.substring(); // 'string'
Copy the code
  1. toLowerCase: converts the string tolowercaseIn the form of
'STRING'.toLowerCase(); // 'string'
Copy the code
  1. toUpperCase: converts the string toA capitalIn the form of
'string'.toUpperCase(); // 'STRING'
Copy the code
  1. trim, trimStart, trimEnd: Removes charactersLeft/right/beginning/endWhitespace character
const spaceStr = ' string ';
spaceStr.trim(); // 'string'
spaceStr.trimStart(); 'string '
spaceStr.trimStart(); ' string'
Copy the code