Latest web development tutorials

JavaScript split () method

String Object Reference JavaScript String Object

Examples

To split a string into an array of strings:

var str="How are you doing today?";
var n=str.split(" ");

n output values of an array:

How,are,you,doing,today?

try it"

Definition and Usage

split () method is used to split a string into an array of strings.

Tip: If the empty string ( "") used as a separator, then stringObject the city is divided between each character.

Note: split () method does not change the original string.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support split () method


grammar

string.split( separator , limit )

Parameter Value

参数 描述
separator 可选。字符串或正则表达式,从该参数指定的地方分割 string Object。
limit 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。

return value

类型 描述
Array 一个字符串数组。该数组是通过在 separator 指定的边界处将字符串 string Object 分割成子串创建的。返回的数组中的字串不包括 separator 自身。

technical details

JavaScript version: 1.1


More examples

Examples

Partition parameter is omitted:

var str="How are you doing today?";
var n=str.split();

n output array worthy results:

How are you doing today?

try it"

Examples

Split each character, including spaces:

var str="How are you doing today?";
var n=str.split("");

n output array worthy results:

H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?

try it"

Examples

Use limit parameters:

var str="How are you doing today?";
var n=str.split(" ",3);

n output 3 array of values:

How,are,you

try it"

Examples

Use a character as a delimiter:

var str="How are you doing today?";
var n=str.split("o");

n output array worthy results:

H,w are y,u d,ing t,day?

try it"


String Object Reference JavaScript String Object