We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

This translation is incomplete. Please help translate this article from English.

Metoda split() divizează un obiect String într-un array de stringuri divizând stringul în substringuri, folosind un separator specificat pentru a determina unde să se facă fiecare divizare.

Sintaxă

str.split([separator[, limit]])

Sfat: Dacă un string gol ("") este folosit ca separator, atunci stringul str va fi divizat între fiecare caracter.

Parametrii

separator Optional
Specifică un string care reprezintă punctele unde ar trebui să se facă fiecare divizare. Parametrul separator este tratat ca un string sau ca o expresie regulată. Dacă un separator-text conține mai mult de un caracter, acest string în întregime va fi implicat în căutare pentru a reprezenta punctul de divizare. Dacă separator-ul este omis sau nu apare în str,  array-ul returnat conține un element format dintr-un singur string. Dacă separator-ul este un string gol,  str este convertit într-un array de caractere.
limit Optional

Un număr întreg care specifică limita divizărilor care pot fi găsite. Atunci când acest parametru este prevăzut, metoda split() divizează stringul la fiecare apariție a separator-lui specificat, dar se oprește când intrările au ajuns la limit și au fost introduse în array. Acest array poate conține mai puține intrări decât limit dacă se ajunge la sfârșitul stringului înainte da atingerea limitei specificate. Textul rămas nu este returnat în noul array.

Valoarea returnată

Un Array de stringuri divizate la fiecare punct unde separatorul are loc în stringul dat.

Description

When found, separator is removed from the string and the substrings are returned in an array. If separator is not found or is omitted, the array contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters. If separator appears at the beginning or end of the string, or both, the array begins, ends, or both begins and ends, respectively, with an empty string. Thus, if the string consists solely of one instance of separator, the array consists of two empty strings.

If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.

Note: When the string is empty, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

Examples

Using split()

The following example defines a function that splits a string into an array of strings using the specified separator. After splitting the string, the function logs messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements.

function splitString(stringToSplit, separator) {
  var arrayOfStrings = stringToSplit.split(separator);

  console.log('The original string is: "' + stringToSplit + '"');
  console.log('The separator is: "' + separator + '"');
  console.log('The array has ' + arrayOfStrings.length + ' elements: ' + arrayOfStrings.join(' / '));
}

var tempestString = 'Oh brave new world that has such people in it.';
var monthString = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';

var space = ' ';
var comma = ',';

splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);

This example produces the following output:

The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.

The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it.

The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec

Removing spaces from a string

In the following example, split() looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces and the semicolon from the string. nameList is the array returned as a result of split().

var names = 'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';

console.log(names);

var re = /\s*;\s*/;
var nameList = names.split(re);

console.log(nameList);

This logs two lines; the first line logs the original string, and the second line logs the resulting array.

Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand " ]

Returning a limited number of splits

In the following example, split() looks for spaces in a string and returns the first 3 splits that it finds.

var myString = 'Hello World. How are you doing?';
var splits = myString.split(' ', 3);

console.log(splits);

This script displays the following:

["Hello", "World.", "How"]

Capturing parentheses

If separator contains capturing parentheses, matched results are returned in the array.

var myString = 'Hello 1 word. Sentence number 2.';
var splits = myString.split(/(\d)/);

console.log(splits);

This script displays the following:

[ "Hello ", "1", " word. Sentence number ", "2", "." ]

Reversing a String using split()

var str = 'asdfghjkl';
var strReverse = str.split('').reverse().join(''); // 'lkjhgfdsa'
// split() returns an array on which reverse() and join() can be applied

Bonus: use === operator to test if the original string was palindrome.

Specifications

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.1.
ECMAScript 5.1 (ECMA-262)
The definition of 'String.prototype.split' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.split' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.prototype.split' in that specification.
Draft  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes1 Yes Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4 Yes Yes Yes

See also

Document Tags and Contributors

Contributors to this page: radxene
Last updated by: radxene,