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

string.split(delimiter,count)

delimiter : Optional, the separation character

count : Optional, number of split items

example

	var str = "Javascript Split() Method";
	var arr = str.split(" ",2);

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

<html>
<body>
<script type="text/javascript">
	var str = "Javascript Split() Method";
	var arr = str.split(" ",2);
	document.write(arr);
</script>
</body>
</html>

More Split Examples

	var str = "Javascript Split() Method";
	var arr = str.split(" ");

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

	var str = "Javascript Split() Method";
	var arr = str.split("t");

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

	var str = "Javascript Split() Method";
	var arr = str.split();

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

str.concat(string1, string2[, ..., stringN])

example

var str1 = "Javascript";
var str2 = "Concat";
var str3 = "method";
var cStr = "";
document.write(cStr.concat(str1," ",str2," ",str3));

The above code will return “Javascript Concat method”

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.

	var str = "Javascript indexof method";
	str.indexOf("method");

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

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.

	var str = "i am very very happy";
	str.lastIndexOf("very");

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