Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript splitting a string in an array

I'm new to javascript and had a quick question.

Here's what I'm trying to accomplish.

myArray = ["Jack Ripper", "John Parker"]; 

I'd like to return an array by breaking up the input string into individual words. For example "Jack Ripper" would be returned as ["Jack", "Ripper"]

I don't know what this is called but would sure like to learn. Any help or a point in the right direction would be much appreciated.

like image 496
12hythm Avatar asked May 25 '26 14:05

12hythm


1 Answers

In general this is called string splitting. Pretty much every language will have built in tools to do this. In my example I have used a space (" ") as my delimiter, but you can split a string by pretty much anything. For example, if you had "Jack,Ripper" you could use split(",").

var str = "Jack Ripper";
var result = str.split(" ");

produces

result[0] = Jack

result[1] = Ripper

You should be able to use this example to do whatever you need concerning larger strings, different delimiters, etc...

like image 77
takendarkk Avatar answered May 28 '26 04:05

takendarkk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!