views:

3292

answers:

5

I have this string 'john smith~123 Street~Apt 4~New York~NY~12345'

Using javascript what is the fastest way to parse this into

var name = "john smith"; var street= "123 Street";

etc

+12  A: 

With simple JavaScript:

var split = 'john smith~123 Street~Apt 4~New York~NY~12345'.split('~');

var name = split[0];
var street = split[1];

etc...
Zach
+1  A: 

You'll want to look into JavaScript's substr or split as this is not really a task suited for jQuery

John Sheehan
+2  A: 

You don't need jQuery.

var s = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = s.split(/~/);
var name = fields[0];
var street = fields[1];
Grant Wagner
+1  A: 

well, easiest way would be something like:

var address = theEncodedString.split(/~/)
var name = address[0], street = address[1]
Dan
+1  A: 

Something like:

var divided = str.split("/~/");
var name=divided[0];
var street = divided[1];

Is probably going to be easiest

Steve g