Split a String into an Array Using the split Method

The split method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and…


This content originally appeared on DEV Community and was authored by Randy Rivera

  • The split method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.

  • Here are two examples that split one string by spaces, then another by digits using a regular expression:

let str = "Hello World";
let byWords = str.split(" ");

let otherString = "How9are7you2today";
let byDigits = otherString.split(/\d/);

// byWords would have the value ["Hello", "World"]
// byDigits would have the value ["How", "are", "you", "today"]
  • Now let's use the split method inside the splitify function to split str into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation.
function splitify(str) {
  // Only change code below this line


  // Only change code above this line
}
splitify("Hello World,I-am code");
  • If you remember from my other posts simply split the string to create a new array of words. A simple regular expression can be used to achieve this result.
  • /\W/ Matches any non-word character. This includes spaces and punctuation, but not underscores. It’s equivalent to /[^A-Za-z0-9_]/

  • Answer:

function splitify(str) {
let splitWords = str.split(/\W/)
return splitWords

}
console.log(splitify("Hello World,I-am code"));

Larson, Quincy, editor. “Split a String into an Array Using the split Method.” Https://Www.freecodecamp.org/, Class Central, 2014, twitter.com/ossia.


This content originally appeared on DEV Community and was authored by Randy Rivera


Print Share Comment Cite Upload Translate Updates
APA

Randy Rivera | Sciencx (2021-07-01T21:22:11+00:00) Split a String into an Array Using the split Method. Retrieved from https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-split-method/

MLA
" » Split a String into an Array Using the split Method." Randy Rivera | Sciencx - Thursday July 1, 2021, https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-split-method/
HARVARD
Randy Rivera | Sciencx Thursday July 1, 2021 » Split a String into an Array Using the split Method., viewed ,<https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-split-method/>
VANCOUVER
Randy Rivera | Sciencx - » Split a String into an Array Using the split Method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-split-method/
CHICAGO
" » Split a String into an Array Using the split Method." Randy Rivera | Sciencx - Accessed . https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-split-method/
IEEE
" » Split a String into an Array Using the split Method." Randy Rivera | Sciencx [Online]. Available: https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-split-method/. [Accessed: ]
rf:citation
» Split a String into an Array Using the split Method | Randy Rivera | Sciencx | https://www.scien.cx/2021/07/01/split-a-string-into-an-array-using-the-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.