Creating Arrays from Strings

E

evanburen

How would I put 2 strings like this into 2 separate arrays like my
examples below? Thanks.

Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9

France|Germany|Norway


var DivOrder = new
Array("Div10,Div11","Div2,Div3,Div8,Div4,Div12,Div1","Div3,Div5,Div9");
var DivNames = new Array("France", "Germany", "Norway");
 
U

UnaCoder

Easy Piesy lemon squeezy!

just .split!

foo = "Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9";
bar = foo.split("|");

now bar[0] = "Div10,Div11"
and bar[1] = "Div2,Div3,Div8,Div4,Div12,Div1,Div5|"
and you get the idea.
 
J

Jambalaya

How would I put 2 strings like this into 2 separate arrays like my
examples below? Thanks.

Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9

France|Germany|Norway


var DivOrder = new
Array("Div10,Div11","Div2,Div3,Div8,Div4,Div12,Div1","Div3,Div5,Div9");
var DivNames = new Array("France", "Germany", "Norway");

var orderstr =
'Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9';
var namestr = 'France|Germany|Norway';

var DivOrder = orderstr.split('|');
var DivNames = namestr.split('|');
 
U

UnaCoder

Oh, and if you want to do the opposite, say:

var DivNames = new Array("France", "Germany", "Norway");
var I_hate_arrays = DivNames.join("|");

now I_hate_arrays == "France|Germany|Norway"
 
E

evanburen

Great, thanks. What if I had one long string like this that I separated
into 2 sections with a ~ character.

Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9~France|Germany|Norway


How would I get this string into 2 arrays

var DivOrder = new
Array("Div10,Div11","Div2,Div3,Div8,Div4,Div12,Div1","Div3,Div5,Div9");

var DivNames = new Array("France", "Germany", "Norway");
 
Z

Zif

Great, thanks. What if I had one long string like this that I separated
into 2 sections with a ~ character.

Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9~France|Germany|Norway


How would I get this string into 2 arrays

var DivOrder = new
Array("Div10,Div11","Div2,Div3,Div8,Div4,Div12,Div1","Div3,Div5,Div9");

var DivNames = new Array("France", "Germany", "Norway");

var srcString = "Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,"
+ "Div5|Div3,Div5,Div9~France|Germany|Norway";
var temp = srcString.split('~');
var DivOrder = temp[0].split('|');
var DivNames = temp[1].split('|');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top