473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Creating Arrays from Strings

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|Di v3,Div5,Div9

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

Feb 15 '06 #1
5 1143
Easy Piesy lemon squeezy!

just .split!

foo = "Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|D iv3,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.

Feb 15 '06 #2
ev*******@gmail.com wrote:
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|Di v3,Div5,Div9

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


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

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

Feb 15 '06 #3
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"

Feb 15 '06 #4
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|Di v3,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,Div 1","Div3,Div5,Div9");

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

Feb 16 '06 #5
Zif
ev*******@gmail.com wrote:
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|Di v3,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,Div 1","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('|');
--
Zif
Feb 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
2
by: Steve | last post by:
I want an initializer for an array of pointers to arrays of strings. So I can do something like this: const char *t1 = { "a", "b", "c", NULL }; const char *t2 = { "p", "q", NULL }; const char...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.