What is the equivalent of the explode function in Javascript?

It would be to use the function split(separator), which is used on variables that store a string or chain. Its argument or input parameter is the character or string that will separate the terms or tokens that must be obtained.

As return, the function returns an array with the extracted data.

For example, a line from a CSV file as a text string:

code, concept, description

Separating this line by the comma separator ( “,” ) you can use the split function conceptually like this:

arrayOfTerms = string.split(‘,’); // contains first term ‘code’ arrayOfTerms // contains first term ‘concept’ arrayOfTerms // contains first term ‘description’ arrayOfTerms

A practical example:

var string = “we are here to learn”; var arrTerms = string.split(‘ ‘); console.log(‘total terms:’ + arrTerms.length); console.log(‘first term:’ + arrTerms); console.log(‘last term:’ + arrTerms);

Another example would be using the function implicitly:

var arrTerminos = “we are here to learn”.split(‘ ‘);

After that code the variable arrTerminos would be an array of 4 cells like this:

See also  Show calendar PHP I
Loading Facebook Comments ...
Loading Disqus Comments ...