Thursday, April 12, 2018

Array - Split and join

A delimiter should be used with split method and its an optional parameter.

Syntax: 
string.split(separator, limit)


separator - Optional. Specifies the character, or the regular expression, to use for splitting the string.

limit - Optional. An integer that specifies the number of splits. No item will be take care after specified limit.

Example I:



let names = 'Test, Test1, Test3';

let arr = names.split(', ');

for (let name of arr) {
  alert( name ); // Test Test1 Test3
}

Example II:


var strTest = "How are you Baby?";
 var resTest = str.split(" ", 3);
 alert(resTest); // How,are,you

Example III:

Nothing specified then , below result you will get.

let str = "test";

alert( str.split('') ); // t,e,s,t


Join Method:

The reverse of split is called join method. Means, opposite to reverse.


let arr = ['Test1', 'Test2', 'Test3'];

let str = arr.join(';');

alert( str ); //Test1;Test2;Test3







No comments:

Post a Comment