string parsing

S

samuelberthelot

Hi,
I can't figure out how to parse the following string and to decompose
it into substring.

CuttingFlags--C500F2*#C503F3*#C509F10*#C506F2*F15##

Basically, each C should go in an array. and for each C I should have
an array of F.

CuttingFlags array contains C500, C503, C509 and C506

C500 contains F2, C506 contains F2 and F15.....

Can you help to parse the string first ?

Thanks
 
B

Bart Van der Donck

I can't figure out how to parse the following string and to decompose
it into substring.

CuttingFlags--C500F2*#C503F3*#C509F10*#C506F2*F15##

Basically, each C should go in an array. and for each C I should have
an array of F.

CuttingFlags array contains C500, C503, C509 and C506

C500 contains F2, C506 contains F2 and F15.....

Can you help to parse the string first ?

I believe the following should do the trick:

var str = 'C500F2*#C503F3*#C509F10*#C506F2*F15##';
var spl = str.split('#');
var results = new Array();

for (i=0; i<spl.length; ++i) {
if (spl && spl != '') {
var k = spl.split('F');
k[0] = k[0].replace(/^C/,'');
results[k[0]] = 'F'+k[1];
for (j=2; j<k.length; ++j) {
results[k[0]]+='F'+k[j];
}
}
}

for (i=0; i<results.length; ++i) {
if (results && results!='') {
var es = results.split('*');
document.writeln('<hr><big>C'+i+' contains:</big><br>');
for (j=0; j<es.length; ++j) {
if (es[j] && es[j]!='') {
document.writeln(es[j]+'<br>');
}
}
}
}

Hope this helps,
 
S

Seige

That was fun! :p

I can't figure out how to parse the following string and to decompose
it into substring.

CuttingFlags--C500F2*#C503F3*#C509F10*#C506F2*F15##

Basically, each C should go in an array. and for each C I should have
an array of F.

CuttingFlags array contains C500, C503, C509 and C506

C500 contains F2, C506 contains F2 and F15.....

Can you help to parse the string first ?

I believe the following should do the trick:

var str = 'C500F2*#C503F3*#C509F10*#C506F2*F15##';
var spl = str.split('#');
var results = new Array();

for (i=0; i<spl.length; ++i) {
if (spl && spl != '') {
var k = spl.split('F');
k[0] = k[0].replace(/^C/,'');
results[k[0]] = 'F'+k[1];
for (j=2; j<k.length; ++j) {
results[k[0]]+='F'+k[j];
}
}
}

for (i=0; i<results.length; ++i) {
if (results && results!='') {
var es = results.split('*');
document.writeln('<hr><big>C'+i+' contains:</big><br>');
for (j=0; j<es.length; ++j) {
if (es[j] && es[j]!='') {
document.writeln(es[j]+'<br>');
}
}
}
}

Hope this helps,
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top