String method

  1. Gets a certain character of the string

[] === charAt

let str = "abcdefg"; console.log(str[0]); //"a"console.log(str.charAt(1)); //"b"Copy the codeCopy the code
  1. Get the length of the string

str.length

let str = "abcdefg"; console.log(str.length); //7 console.log(str[str.length-1]); //"g"Copy the codeCopy the code
  1. Searches for a character, returns the position of the first match if there is one, returns -1 otherwise

indexOf()

let str = "abcdefg";
console.log(str.indexOf("a")); //0 console.log(str.indexOf("z")); //-1 copies the codeCopy the code
  1. Converts characters to ASCLL code values and asCLL code values to characters

CharCodeAt (), String. FromCharCode ()

let str = "abcdefg"; console.log(str.charCodeAt(0)); //"a"--> 97 console.log(String.fromCharCode(97)); / / 97 - >"a"Copy the codeCopy the code
  1. String splicing

concat()

let str = "abc";
console.log(str.concat("efg")); //"abcefg"
console.log(str.concat("efg"."hijk")); //"abcefghijk"Copy the codeCopy the code
  1. String cutting
  • slice()
  • substring()
  • substr()
let str = "abcdefg"; / * * / slice the console. The log (STR. Slice (1, 6)); //"bcdef"[1, 6) the console log (STR. Slice (1)); / /"bcdefg"[1,str.length-1] console.log(str.slice()); //"abcdefg"[0,str.length-1] console.log(str.slice(-2)); //"fg"console.log(str.slice(6, 1)); //""/* Slice passes only two values, indicating that the value is closed and the value is open. If a value is passed, indicating that the value does not include the end position, it indicates that the beginning bit is started. The first digit of the string is -1 */ /*substring*/ console.log(str.substring(1,6)); //"bcdef"[1, 6) the console log (STR. The substring (1)); / /"bcdefg"[1,str.length-1] console.log(str.substring()); //"abcdefg"The console [0, STR. Length - 1]. The log (STR) the substring (6, 1)); //"bcdef"[1, 6) the console log (STR. The substring (1)); / /"abcdefg"/* The same usage as slice, except that the first value passed in for substring is larger than the second value, and the negative value is passed in by default. */ /*substr(start,length)*/ console.log(str.substr(1,6)); //"bcdefg"6 represents cut length console.log(str.substr(1)); //"bcdefg"[1,str.length-1] console.log(str.substr()); //"abcdefg"[0,str.length-1] console.log(str.substr(-1)); //"g"/* substr The first argument indicates the starting position of the cut and the second argument indicates the length of the cutCopy the code
  1. String conversion case
  • toUpperCase()
  • toLowerCase()
let str1 = "adABDndj"; console.log(str1.toUpperCase()); //"ADABDNDJ"console.log(str1.toLowerCase()); //"adabdndj"Copy the codeCopy the code
  1. The cut string returns an array

The split() method contrasts with the array arr.join() method

Note: All methods on a string do not change the string itself

Array methods

  1. Array values

Array with subscript value [idx]

letArr = [1, 2, 3, 4]; console.log(arr[1]); //2 Copy the codeCopy the code
  1. Array concatenation

arr.concat()

letArr = [1, 2, 3]; The console. The log (arr. Concat ([4 and 6], [7,8,9])); //[1, 2, 3, 4, 5, 6, 7, 8, 9] copy codeCopy the code
  1. Gets the length of the array

arr.length

  1. useslicePerform array slicing

Same usage as string

let,2,3,4,5,6,7,8 arr = [1]; The console. The log (arr. Slice (1, 6)); / / [2, 3, 4, 5, 6] [1, 6). The console log (arr. Slice (1)); / / [2, 3, 4, 5, 6, 7, 8] [1, arr. Length - 1] console. The log (arr. Slice ()); / / [1, 2, 3, 4, 5, 6, 7, 8] [0, arr. Length - 1]. The console log (arr. Slice (2)); / / [7, 8]. The console log (arr. Slice (6, 1)); / / [] to copy codeCopy the code
  1. indexOfTo find the
let,2,3,4,5,6,7,8 arr = [1]; console.log(arr.indexOf(8)); //7 console.log(arr.indexOf(10)); //-1 copies the codeCopy the code
  1. To add or remove data from an array
  • pushAdd data to the end of the array
  • unshiftAdd data to the beginning of the array
  • popDeletes the data at the end of the array
  • shiftDeletes the first data at the beginning of the array
letArr = 5-tetrafluorobenzoic [1]; arr.push("a", 6); console.log(arr); / / [1, 5-tetrafluorobenzoic,"a"6]; arr.pop(); console.log(arr); / / [1, 5-tetrafluorobenzoic,"a"]; arr.unshift(2); console.log(arr); / / [2,1,3,4,5,"a"]; arr.shift(); console.log(arr); / / [1, 5-tetrafluorobenzoic,"a"]; Copy the codeCopy the code
  1. arr.splice()You can implement all the functions of the above four methods
  • Parameter 1 specifies whether to add or remove the starting index
  • Parameter 2 specifies the length to be removed (this value must be 0 to indicate that no elements are removed)
  • Parameter 3 and subsequent parameters indicate the data to be added (optional)
letArr = 5-tetrafluorobenzoic [1]; Arr. Splice (0, 0,"12"); //===arr.unshift("12"); console.log(arr); / / /"12",1, 3, 4, 5] arr. Splice (0,1); //=== arr.shift(); console.log(arr); //[1, 3, 4, 5] arr.splice(arr.length,0,"a"); //===arr.push("a"); console.log(arr); / / [1, 3, 4, 5,"a"] arr. Splice (arr. Length - 1, 1); //===arr.pop(); console.log(arr); //[1, 3, 4, 5] copy codeCopy the code
  1. Sorting of arrays (typically for data of type number)

arr.sort()

letArr =,3,67,42,100,21 [97]; Arr.sort ((a,b)=>a-b); console.log(arr); / / [3, 21, 42, 67, 97, 100] / / descending sort arr. Sort ((a, b) = > b-a); console.log(arr); [100, 97, 67, 42, 21, 3] copy the codeCopy the code
  1. Arrays are concatenated into strings

arr.join()

let,23,4,45,5 arr = [1];let str = arr.join("-"); console.log(str); //"The 1-23-4-45-5"// Cut the string into an arraylet arr1= str.split("-"); Console. log(arr1); / / /"1"."23"."4"."45"."5"Copy codeCopy the code
  1. Antitone array

arr.reverse()

let,3,4,5,6,7 arr = [1]; arr.reverse(); console.log(arr); //[7, 6, 5, 4, 3, 1] copy codeCopy the code

Note: there is one more method that has the same name as the stringjoin()They don’t change the original array