1

What is the best way to split a string into array objects? I would like the following string:

var str = "New York, California, Indiana"

to turn into the following array

var arr =  ["New York", "California", "Indiana"]

Please note: the string includes spaces and commas that I would like to disregard, but some spaces need to be retained (i.e. space between "New York"). What would be the best way to accomplish this?

1
  • 1
    str.split(', ') Commented Jul 27, 2017 at 15:49

2 Answers 2

1

You can use str.split with the split parameter as ', ' so it will split the strings with their space values:

var str = 'New York, California, Indiana';
var arr = str.split(', ');
alert(arr);

Sign up to request clarification or add additional context in comments.

Comments

0

Should be possible with a String Split

var str = "New York, California, Indiana"
var arr= str.split(",");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.