JavaScript tutorial:
split method

 

Applies to: String Object

The split method returns the array of strings that results when a string is separated into substrings.

Syntax

stringObj.split(str)

The split method syntax has these parts:

Part

Description

stringObj

Required. The String object or literal to be split. This object is not modified by the split method.

str

Required. A string or Regular Expression object describing what character is used to define where the splits take place.

Return value

The split method returns the array of strings that results when a string is separated into substrings.

Example

The result of the split method is an array of strings split at each point where str occurred in stingObj.

The following example illustrates the use of the split method:

function splitString(str,splitStr)
{
var ss;
// Split at each space character.
ss = str.split(splitStr);
return(ss);
}
document.write(splitString( "SuperMan BatMan CatWoman GrazierMan",""));

  To run the code, paste it into JavaScript Editor, and click the Execute button.