String .split() Method

In MDN, the definition is – “The .split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array”.

We use this method to split a string with a pattern stated in the first parameter, and …


This content originally appeared on DEV Community and was authored by Cindy Lam

In MDN, the definition is - "The .split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array".

We use this method to split a string with a pattern stated in the first parameter, and limits in the second parameter. But they are not required.

From the examples below, please use console.log(splits) to see the output in the console.

const words = 'Hello, I am a Split!'; 

let splits = words.split(); //no parameters
//Output: ['Hello, I am a Split!']

let splits = words.split(' '); //a whitespace
//Output: ['Hello,', 'I', 'am', 'a', 'Split!']

let splits = words.split(','); //a comma
//Output: ['Hello', ' I am a Split!']

let splits = words.split(' ', 3);
//Output: ['Hello,', 'I', 'am']

Split method can also have multiple parameters:

  • We need to use slashes instead of quotations within the split method when there are multiple parameters since we are using regex (Regular Expressions).
const words = 'Hello, I am a Split!'; 

//Using Regex - brackets '/[]/'
let splits = words.split(/[,\s!]/); //comma, whitespace ('\s'), exclamation
//Output: ['Hello', '', 'I', 'am', 'a', 'Split', '']

//Using Regex - pipes '/|/'
let splits = words.split(/,|\s|!/);
//Output: ['Hello', '', 'I', 'am', 'a', 'Split', '']

Additional Notes:

As you noticed there are some empty elements generated from the output, you can use Array filter() method to get rid of them, as below:

const filters = splits.filter(element => element); 
//Output: ['Hello', 'I', 'am', 'a', 'Split']


This content originally appeared on DEV Community and was authored by Cindy Lam


Print Share Comment Cite Upload Translate Updates
APA

Cindy Lam | Sciencx (2022-01-31T01:26:30+00:00) String .split() Method. Retrieved from https://www.scien.cx/2022/01/31/string-split-method/

MLA
" » String .split() Method." Cindy Lam | Sciencx - Monday January 31, 2022, https://www.scien.cx/2022/01/31/string-split-method/
HARVARD
Cindy Lam | Sciencx Monday January 31, 2022 » String .split() Method., viewed ,<https://www.scien.cx/2022/01/31/string-split-method/>
VANCOUVER
Cindy Lam | Sciencx - » String .split() Method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/31/string-split-method/
CHICAGO
" » String .split() Method." Cindy Lam | Sciencx - Accessed . https://www.scien.cx/2022/01/31/string-split-method/
IEEE
" » String .split() Method." Cindy Lam | Sciencx [Online]. Available: https://www.scien.cx/2022/01/31/string-split-method/. [Accessed: ]
rf:citation
» String .split() Method | Cindy Lam | Sciencx | https://www.scien.cx/2022/01/31/string-split-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.