Array and string methods

Welcome to wechat public account [front-end gongcheng House] to share some front-end technology, interview questions, interview skills and so on

Common methods for strings

Finds the value of the string based on the index value

String. charAt(index) returns the value of the character at the given position

  • Parameters:indexRefers to a number at a position in the string, that is, the subscript of a character in the string
  • Return value: a stringstringThe firstindexThe character corresponding to the index
  • "ABC". The charAt (2) // c
    Copy the code

Finds the quoted value based on the character value

String.indexof (substring, start) Searches for the specified substring from a string, returning the position of the substring (-1 if not found)

  • Parameters:
    • substring: must be in the stringstringA substring retrieved from
    • start: an optional integer argument declared in a stringstringIs the location where the search begins. Its default value is0, starting at the first character of the string.
  • Return value: YesstringIn thestartAfter the position,substringThe first value is zerostringThe position of the first occurrence in. If not found, return- 1
  • 'abcdefghijk. IndexOf (" ABC ",5) // -1
    Copy the code

String.lastindexof (subString, start) Retrieves a substring from back to front

  • Parameters as above
  • Return value: YesstringIn thestartBefore the position,substringThe first value is zerostringIf not found, returns the first occurrence of the location- 1

String.search () retrieves substrings that match the regular expression

  • Parameters:
    • regexpTo set the stringstringRetrieve theRegExpObject that has the specified schema
    • If the parameter is notRegExpObject is first passed toRegExp()Constructor to convert it toRegExpObject.
  • The return value:stringThe first of the tworegexpThe starting position of the matching substring. Returns if no matching substring is found- 1
  • "Abcdefg". The search (' DE ')/ / 3
    Copy the code

Method of intercepting a string

String. slice(start, end) intercepts a substring

  • Parameters:
    • start: Indicates the starting position of the index. Negative if I count backwards,- 1Represents the index of the last digit
    • end: Indicates the end of the index. If not, the last bit is intercepted by default
    • The return value:
      • A new string
      • fromstartStart (includingstart) toendUp to (excludingend)
    • 'abcdefghijklm'. Slice (2.5) / / 'ced
      Copy the code

String. substr(start, length) extracts a substring

  • Parameters:
    • start: Indicates the starting position of the index. Negative if I count backwards,- 1Represents the index of the last digit.
    • length: The number of characters in a substring. If not specifiedlength, returns a string containing a string fromstartThe character to the end.
  • The return value:
    • A new string
    • fromstringstart(includingstartThe character indicated)lengthA character
  • 'hello world' substr (.3.7) / / 'lo worl'
    Copy the code

String. substring(from, to) Returns a substring of the string

  • Parameters:
    • from: an integer, truncating the starting index position
    • to: Indicates the end of the index. If not, the last bit is intercepted by default
  • The return value
    • A new string of lengthto-from, fromfromStart (includingfrom) totoUp to (excludingto)
  • 'hello world'. The substring (3.7) / / 'lo w'
    Copy the code

String substitution

string.replace(regexp, replacement)

  • stringstringThe method ofreplace()You perform a find and replace operation
  • Parameters:
    • regexp: declares the schema to be replacedRegExpObject. If the parameter is a string, it is treated as the direct volume text mode to retrieve, rather than being converted to firstRegExpObject.
    • replacement: a string that declares the function that replaces or generates the replacement text
  • "Abcdefg". The replace (" ab ", the "CD")/ / 'cdcdefg'
    Copy the code

Traversal lookup of a string

String.match (regexp) Finds matches for one or more regular expressions

  • Return value: Holds the array of matches
  • 'abced'. The match (' a ')/ / / 'a'
    Copy the code

String to array

String.split (delimiter, limit) splits a string into an array of strings

  • Parameters:
    • delimiter:Must have!String or regular expression drawn from this parameter is segmentedstring
    • limit: This optional integer specifies the maximum length of the returned array
    • If this parameter is set, no more substrings will be returned than the number specified by this parameter
    • If this parameter is not set, the entire string is split regardless of its length
  • Return value: an array of strings that are passed indelimiterSpecifies the boundary at which the string will bestringCreated by splitting into substrings. Substrings in the returned array are not includeddelimiterTheir own
  • Pay attention to:String.split()Operations performed andArray.join()Do the opposite.
  • "ABC". The split (' ')/ / [' a ', 'b', 'c']
    Copy the code

Common methods in arrays

Add items to the array

array.push(value, …) Add one or more elements to the end of the array

  • Parameters:value, to add toarrayThe tail value can be one or more
  • Return value: The new length of the array after the specified value is added to the array

array.unshift(value, …) Adds one or more elements to the front of the array

  • Parameters:valueTo insert one or more values in the array header
  • Return value: The new length of the array after the specified value is added to the array

Deletes an item from an array

Array.pop () removes and returns the last element of the array

  • Parameters: no
  • The return value:arrayThe last element of
  • methodspop()Will deletearrayThe last element of the, subtract the array length1, and returns the value of the element it deleted.
  • If the array is already emptypop()Returns without changing the arrayundefined

Array.shift () moves the first element of the array out of the array

  • Parameters: no
  • Return value: the original first element of the array
  • methodsshift()Will thearrayRemoves the first element from the array, returns the value of that element, and moves all remaining elements forward one bit to fill the hole in the array header.
  • If the array is empty,shift()Returns without doing anythingundefined
  • methodsshift()And methodspop()Similar, except that it operates at the head of the array instead of the tail. The method often andunshift()Used together

Changing an array entry

Array.reverse () reverses the order of elements in an array

  • arrayObject methodsreverse()Reverses the position of the elements in the array.
  • It does this on the original array, rearranging the specifiedarrayElement, but does not create a new array.
  • If thearrayThere are multiple references, and you can see the new order of the array elements through all of them.

Array.sort (function) sorts array elements

  • Parameters:functionYou can control the sorting of numbers
  • Return value: a reference to an array.
  • Note: Arrays are sorted on top of the original array, no copies are made
  • If you call a methodsort()The elements in the array are sorted alphabetically (in character encoding order)

Query an array item

  • indexOf()
  • lastIndexOf()
  • These two attributes refer to the definition and introduction of strings

Through the array

array.forEach(item[, thisObject])

  • Parameters:
    • item: each element in the function argument array
    • thisObjectThe: object is used when the callback is executed
  • Return value: Creates an array

array.map(item[, thisObject])

  • Method returns a new array consisting of the return value of each element in the original array after calling a specified method

The difference between forEach and map

ForEach is traversal, and map is mapping

Intercepts an array value

Array. slice(start, end) returns a portion of the array

  • Parameters:
    • start: Array subscript at the beginning of the array fragment. If it is negative, it declares the position from the end of the array
    • end: Array index of the last element at the end of the array fragment
    • If not specifiedend, the default value is fromstartAll elements from the beginning to the end of the array.
  • Return value: a new array containing the values fromstartend(excluding the element) specifiedarrayThe element
  • Pay attention to: Do not change the original array! If you want to delete an element from an array, use thearray.splice

array.splice(start, length, value, …) Inserts, removes, or replaces elements in an array

  • Parameters:
    • start: The index of the array element to start inserting or deleting
    • lengthFrom:startBeginning, includingstartIndicates the number of elements within the element to be deleted. If not specifiedlength.splice()Will be removed from thestartStart all elements up to the end of the original array.
    • value: To insert zero or more values into the array fromstartThe insertion begins at the subscript indicated.
  • Return value: if fromarray, returns an array of the deleted elements.

Other array methods

array.concat(value, …) Splice array

  • Parameters:The value of... .To add to thearrayCan be any number of values
  • Return value: a new array
  • methodsconcat()Will create and return a new array that adds all the parameters toarrayGenerated in.
  • Do not modify the original arrayarray
  • If we were to proceedconcat()The operation takes an array, so you’re adding elements to the array, not the array.

Array.join (separator) concatenates array elements to build a string

  • Parameters:separatorOptionally, the character or string used to split array elements in the returned string. If this parameter is omitted, use a comma as a delimiter.
  • Return value: a string passed through thearrayConvert each element of the string to a string, then concatenate the strings and insert them between the two elementsseparatorString generated.

Hope to read this article you have help, have inspiration, if there is not enough, welcome to criticize the exchange!

Welcome to wechat public account [front-end gongcheng House] to share some front-end technology, interview questions, interview skills and so on

Hard to sort out for a long time, but also hope to manually praise encouragement ~


‘Extract’ is not simply “paste -> copy”, but eye to, hand to, heart to beat down every word.

Statement: all reprinted articles and pictures are only used for the author’s own collection and study purposes. If requested or deemed appropriate, attribution and source will be indicated. If you do not want a work to be re-used, please inform the site in time, the site will be deleted in time.