string parsing

Discussion in 'Javascript' started by samuelberthelot@googlemail.com, Jun 21, 2006.

  1. Guest

    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
     
    , Jun 21, 2006
    #1
    1. Advertising

  2. wrote:

    > 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,

    --
    Bart
     
    Bart Van der Donck, Jun 21, 2006
    #2
    1. Advertising

  3. Seige Guest

    That was fun! :p


    Bart Van der Donck wrote:
    > wrote:
    >
    > > 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,
    >
    > --
    > Bart
     
    Seige, Jun 22, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    930
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    611
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    626
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    455
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    763
    John Levine
    Feb 2, 2012
Loading...

Share This Page