string.split()

Javascript String split()

Javascript string split() method split the given string to an array, where split is determined by the delimiter that you pass to the method. If the separation parameter is not provided, the entire string is returned as the first element in an array.

Syntax

delimiter : Optional, the separation character

count : Optional, number of split items

example

The above method reruns an array with 2 values.

Javascript Split()

In str.split(" ",2), delimiter is a space (" ") character and limit is 2. So it split with space character and returned 2 first 2 strings.

Full Source

More Split Examples

The above method returns an array with 3 values, because there is no limit for the array, so it returned all the split characters.

It will return "Javascrip, Spli,() Me,hod", because the split character is "t".

It will return the same string "Javascript Split() Method", because there is no delimiter and limit.

string.concat()

Javascript string concat() method combines one or more strings and returned a new string.

Syntax

example

The above code will return "Javascript Concat method"

string.indexOf()

Javascript String indexOf()

Javascript string indexOf searches the string from the beginning for the giver parameter substring. If it is found the parameter string, it will returns the index of the first character of the first occurrence. If there is no match, it will return -1.

It will return 19, because the substring "method" occur in the 19th position in the str.

string.lastIndexOf()

Javascript String lastIndexOf()

Javascript string lastIndexOf searches the string from the end for the giver parameter substring. If it is found the parameter string, it will returns the index of the first character of the first occurrence. If there is no match, it will return -1.

It will return 10, because the substring "very" last occur in the 10th position in the str.