-1

I have the condition

var a = '2008, 1993';

I want to parse this string into

var b = 2008
var c = 1993

using javascript.

0

3 Answers 3

4
var a = '2008, 1993';
var array = a.split(',');
var b = parseInt(array[0]);
var c = parseInt(array[1]);

http://jsfiddle.net/isherwood/X57KE/

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

Comments

1

The .split() method lets you split up a string into an array of substrings based on a separator that you specify.

var a = "2008, 2009",
    items = a.split(/, ?/),
    b = +items[0],
    c = +items[1];

I would suggest a regex /, ?/ to split on, i.e., a comma followed by an optional space - though if you know there will always be both a comma and a space you could say a.split(", ").

Since you seem to want the individual items as numbers rather than strings I have shown how to convert them using the unary plus operator.

Comments

1

You are looking for split function like this:

var array = '2008, 1993'.split(',');
var b = parseInt(array[0]);
var c = parseInt(array[1]);

or like this:

var a = '2008, 1993'.split(', '), b = a.shift(), c=a.shift()

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.