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.
-
Did my answer solve your problem? Or do you need some more help with it?takendarkk– takendarkk2014年01月20日 07:11:18 +00:00Commented Jan 20, 2014 at 7:11
-
Your question is not clear. You didn't specify the context, i.e. If you're trying to make a function or if it is some sort of procedure that should come after this. 2ndly, you didn't put sample code of what you have tried so far. It is encouraged that you first try to solve the problem yourself and then post what you have tried.Ahamad I Milazi– Ahamad I Milazi2014年11月26日 20:00:42 +00:00Commented Nov 26, 2014 at 20:00
1 Answer 1
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...