From 0537140cb63bafbb687c744904a3f25e9e39312e Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 00:23:56 +0530 Subject: [PATCH] Snippet to convert string to array of words --- snippets/stringToArrayOfWords.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/stringToArrayOfWords.md diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md new file mode 100644 index 000000000..5cc742c60 --- /dev/null +++ b/snippets/stringToArrayOfWords.md @@ -0,0 +1,14 @@ +### stringToArrayOfWords + +Converts a given string into an array of words + +First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver string to array with the space a delimiter. + +```js +const stringToArray = str =>{ + [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.replace(d, "")); + return str.split(" "); +} + +// stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] +``` \ No newline at end of file