How to split a string into an array using javascript

Veröffentlicht am: 12 Mai 2019
auf dem Kanal: The Coding Bus
1,545
15

str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument. The syntax of the function is as follows:

str.split(separator, limit)
Arguments
The first argument to this function is a string which specifies the points where the split has to take place. This argument can be treated as a simple string or as a regular expression. 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.
The second argument to the function 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
play_arrow

brightness_4
script
// JavaScript Program to illustrate split() funtion

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

func();
/script
Output:

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

filter_none
edit
play_arrow

brightness_4
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
Output:
....................................................................................................

Follow Us On Social Media For More Updates -:

Facebook -: facebook.com/TheCodingBus/
Website-: https://thecodingbus.info/
Instagram-:   / sourabhdubey007  
Reddit-:   / sourabhdubey007  
Subscribe TCB -: http://bit.ly/TheCodingBus

........................................................................................................
[It,iS] How to create an app for free using your mobile phone 2019
   • How to create an app for free using your m...  

How to delete history in google chrome 2019 android
   • How to delete history from google chrome [...  

how to create camera app in mit app inventor 2
   • How to Create a Camera App in MIT App Inve...  

how to redirect http to https in Wordpress website
   • how to redirect http to https in Wordpress...  

How to install wordpress in xampp step by step
   • How to install wordpress in xampp step by ...  

How to add youtube video to wordpress website
   • How to install wordpress in xampp step by ...  

how to create child theme in WordPress 2019
   • how to create child theme in WordPress 2019  

sdfhfdghjhjlqwerqwes werte qwexvnkl #USA lsdkfjsdlkfjd dfkajsdlfj lkdjsflkdjfdls The Coding Bus #unitedstates
#usa #TheCodingBus
#TCB #unitedstates #us


Auf dieser Seite können Sie das Online-Video How to split a string into an array using javascript mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer The Coding Bus 12 Mai 2019 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 1,545 Mal angesehen und es wurde von 15 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!