JavaScript String split() Method

Below is the example of the String split() Method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
    // JavaScript Program to illustrate split() function 
      
    function func() { 
        //Original string 
        var str = 'Geeks for Geeks'
        var array = str.split("for"); 
        document.write(array); 
      
    func(); 
    </script> 

    chevron_right

    
    

  • Output:
    Geeks , Geeks
    

str.split() method is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.

Syntax:

str.split(separator, limit)
    Perameters:

  • separator: It is used to specifie the character, or the regular expression, to use for splitting the string. If the separator is unspecified then the entire string becomes one single array element. The same also happens when the separator is not present in the string. If the separator is an empty string (“”) then every character of the string is separated.
  • limit: Defines the upper limit on the number of splits to be found in the given string. If the string remains unchecked after the limit is reached then it is not reported in the array.

Return value
This function returns an array of strings that is formed after splitting the given string at each point where the separator occurs.



Examples for the above function are provided below:

Example 1:

var str = 'It iS a 5r&e@@t Day.'
var array = str.split(" ");
print(array);

Output:

[It,iS,a,5r&e@@t,Day.]

In this example the function split() creates an array of strings by splitting str wherever ” “ occurs.

Example 2:

var str = 'It iS a 5r&e@@t Day.'
var array = str.split(" ",2);
print(array);

Output:

[It,iS]

In this example the function split() creates an array of strings by splitting str wherever ” “ occurs. The second argument 2 limits the number of such splits to only 2.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate split() function
  
function func() {
    //Original string
    var str = 'It iS a 5r&e@@t Day.'
    var array = str.split(" ");
    document.write(array);  
}
  
func();
</script>

chevron_right


Output:

[It,iS,a,5r&e@@t,Day.]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript Program to illustrate split() function
  
function func() {
  
    // Original string
    var str = 'It iS a 5r&e@@t Day.'
  
    // Splitting up to 2 terms
    var array = str.split(" ",2);
    document.write(array); 
}
  
func();
</script>

chevron_right


Output:

[It,iS]

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh