Comparing Strings

Discussion in 'Javascript' started by shankwheat, Apr 16, 2007.

  1. shankwheat

    shankwheat Guest

    I have two strings that I need to compare and modify when there is a
    matching value in the two. If strA = '12937,' then I need to remove
    the value '12937,' from strB and the new value would be strB =
    '12931,12935'


    strA = '12937,'

    strB = '12931,12937,12935' (these could be any values in any order)

    I'm not sure where to begin on this.

    Thanks
    shankwheat, Apr 16, 2007
    #1
    1. Advertising

  2. shankwheat

    Daz Guest

    On Apr 16, 5:08 pm, "shankwheat" <> wrote:
    > I have two strings that I need to compare and modify when there is a
    > matching value in the two. If strA = '12937,' then I need to remove
    > the value '12937,' from strB and the new value would be strB =
    > '12931,12935'
    >
    > strA = '12937,'
    >
    > strB = '12931,12937,12935' (these could be any values in any order)
    >
    > I'm not sure where to begin on this.
    >
    > Thanks


    Hi.

    Try this:

    strB = strB.replace(strA, "");

    That will remove "all occurances" of strA from strB, and set the value
    of strB to the value of strB with the specified string replaced. You
    now have the logic, hopefully you can work with it. :) Also, strA can
    be substituted with a regex, and "" can be substituted with any
    string, or even a function. Hope this helps.

    All the best.

    Daz.
    Daz, Apr 16, 2007
    #2
    1. Advertising

  3. shankwheat

    Lee Guest

    Daz said:
    >
    >On Apr 16, 5:08 pm, "shankwheat" <> wrote:
    >> I have two strings that I need to compare and modify when there is a
    >> matching value in the two. If strA = '12937,' then I need to remove
    >> the value '12937,' from strB and the new value would be strB =
    >> '12931,12935'
    >>
    >> strA = '12937,'
    >>
    >> strB = '12931,12937,12935' (these could be any values in any order)
    >>
    >> I'm not sure where to begin on this.
    >>
    >> Thanks

    >
    >Hi.
    >
    >Try this:
    >
    >strB = strB.replace(strA, "");
    >
    >That will remove "all occurances" of strA from strB, and set the value
    >of strB to the value of strB with the specified string replaced. You
    >now have the logic, hopefully you can work with it. :) Also, strA can
    >be substituted with a regex, and "" can be substituted with any
    >string, or even a function. Hope this helps.


    That will leave an extra comma behind.


    --
    Lee, Apr 16, 2007
    #3
  4. shankwheat

    Daz Guest

    On Apr 16, 5:30 pm, Lee <> wrote:
    > Daz said:
    >
    >
    >
    >
    >
    > >On Apr 16, 5:08 pm, "shankwheat" <> wrote:
    > >> I have two strings that I need to compare and modify when there is a
    > >> matching value in the two. If strA = '12937,' then I need to remove
    > >> the value '12937,' from strB and the new value would be strB =
    > >> '12931,12935'

    >
    > >> strA = '12937,'

    >
    > >> strB = '12931,12937,12935' (these could be any values in any order)

    >
    > >> I'm not sure where to begin on this.

    >
    > >> Thanks

    >
    > >Hi.

    >
    > >Try this:

    >
    > >strB = strB.replace(strA, "");

    >
    > >That will remove "all occurances" of strA from strB, and set the value
    > >of strB to the value of strB with the specified string replaced. You
    > >now have the logic, hopefully you can work with it. :) Also, strA can
    > >be substituted with a regex, and "" can be substituted with any
    > >string, or even a function. Hope this helps.

    >
    > That will leave an extra comma behind.
    >
    > --


    Where?

    var strB = '12931,12937,12935';
    var strA = '12937,'
    alert(strB.replace(strA, ""));

    That above script alerts '12931,12935', which is "exactly" what the OP
    asked for...

    The only problem that could arise is that the is no comma at the end,
    but that can easily be inserted/removed as needed.
    Daz, Apr 16, 2007
    #4
  5. shankwheat

    Evertjan. Guest

    Daz wrote on 16 apr 2007 in comp.lang.javascript:

    > strB = strB.replace(strA, "");
    >
    > That will remove "all occurances" of strA from strB,


    Not 'all', you probably are mixing vbs and js in your mind.

    The idea is a good one!

    Try:

    =================================
    <script type='text/javascript'>

    function myStrPush(st,ad) {
    var re = new RegExp(','+ad,'g');
    st = (','+st).replace(re, '')+','+ad;
    return st.substr(1);
    };

    alert(myStrPush('123,123,456','123'));

    </script>
    =================================


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 16, 2007
    #5
  6. shankwheat

    Daz Guest

    On Apr 16, 5:52 pm, "Evertjan." <> wrote:
    > Daz wrote on 16 apr 2007 in comp.lang.javascript:
    >
    > > strB = strB.replace(strA, "");

    >
    > > That will remove "all occurances" of strA from strB,

    >
    > Not 'all', you probably are mixing vbs and js in your mind.
    >
    > The idea is a good one!
    >
    > Try:
    >
    > =================================
    > <script type='text/javascript'>
    >
    > function myStrPush(st,ad) {
    > var re = new RegExp(','+ad,'g');
    > st = (','+st).replace(re, '')+','+ad;
    > return st.substr(1);
    >
    > };
    >
    > alert(myStrPush('123,123,456','123'));
    >
    > </script>
    > =================================
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    You're right. I don't know why I thought that. I don't even use vbs. I
    always use replace() with a regex, and 9 times out of 10, I use the
    "g" and "m" switch.

    Thanks for pointing that out to me. Apologies to the OP for my
    misinforming you.
    Daz, Apr 16, 2007
    #6
  7. shankwheat

    shankwheat Guest

    On Apr 16, 12:56 pm, "Daz" <> wrote:
    > On Apr 16, 5:52 pm, "Evertjan." <> wrote:
    >
    >
    >
    >
    >
    > > Daz wrote on 16 apr 2007 in comp.lang.javascript:

    >
    > > > strB = strB.replace(strA, "");

    >
    > > > That will remove "all occurances" of strA from strB,

    >
    > > Not 'all', you probably are mixing vbs and js in your mind.

    >
    > > The idea is a good one!

    >
    > > Try:

    >
    > > =================================
    > > <script type='text/javascript'>

    >
    > > function myStrPush(st,ad) {
    > > var re = new RegExp(','+ad,'g');
    > > st = (','+st).replace(re, '')+','+ad;
    > > return st.substr(1);

    >
    > > };

    >
    > > alert(myStrPush('123,123,456','123'));

    >
    > > </script>
    > > =================================

    >
    > > --
    > > Evertjan.
    > > The Netherlands.
    > > (Please change the x'es to dots in my emailaddress)

    >
    > You're right. I don't know why I thought that. I don't even use vbs. I
    > always use replace() with a regex, and 9 times out of 10, I use the
    > "g" and "m" switch.
    >
    > Thanks for pointing that out to me. Apologies to the OP for my
    > misinforming you.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks very much!
    shankwheat, Apr 16, 2007
    #7
  8. shankwheat

    Lee Guest

    Daz said:
    >
    >On Apr 16, 5:30 pm, Lee <> wrote:
    >> Daz said:
    >>
    >>
    >>
    >>
    >>
    >> >On Apr 16, 5:08 pm, "shankwheat" <> wrote:
    >> >> I have two strings that I need to compare and modify when there is a
    >> >> matching value in the two. If strA = '12937,' then I need to remove
    >> >> the value '12937,' from strB and the new value would be strB =
    >> >> '12931,12935'

    >>
    >> >> strA = '12937,'

    >>
    >> >> strB = '12931,12937,12935' (these could be any values in any order)

    >>
    >> >> I'm not sure where to begin on this.

    >>
    >> >> Thanks

    >>
    >> >Hi.

    >>
    >> >Try this:

    >>
    >> >strB = strB.replace(strA, "");

    >>
    >> >That will remove "all occurances" of strA from strB, and set the value
    >> >of strB to the value of strB with the specified string replaced. You
    >> >now have the logic, hopefully you can work with it. :) Also, strA can
    >> >be substituted with a regex, and "" can be substituted with any
    >> >string, or even a function. Hope this helps.

    >>
    >> That will leave an extra comma behind.
    >>
    >> --

    >
    >Where?
    >
    >var strB = '12931,12937,12935';
    >var strA = '12937,'
    >alert(strB.replace(strA, ""));
    >
    >That above script alerts '12931,12935', which is "exactly" what the OP
    >asked for...
    >
    >The only problem that could arise is that the is no comma at the end,
    >but that can easily be inserted/removed as needed.


    I hadn't noticed that you had included the comma in the search string.
    That has a much worse problem, in that it will miss occurances of the
    pattern at the end of the string. Change strA to "12935," and see
    what happens.


    --
    Lee, Apr 16, 2007
    #8
  9. shankwheat

    Guest

    i stumbled onto your problem and thought it would be interesting to
    solve it. Just split up your second string into an array, loop through
    it, and check every value in the array against your first string. If
    the two strings do not match, concatenate the value to a temporary
    string.


    --------

    var strA = "234";
    var strB = "453,234,231";
    var strBSplit = strB.split(",");

    var strBTemp = "";
    for ( str in strBSplit ) {
    if(strBSplit[str] != strA){
    strBTemp += strBSplit[str] + ",";
    }
    }

    // remove last comma
    alert(strBTemp.substring(0,strBTemp.length-1));

    -------
    On Apr 16, 9:08 am, "shankwheat" <> wrote:
    > I have two strings that I need to compare and modify when there is a
    > matching value in the two. If strA = '12937,' then I need to remove
    > the value '12937,' from strB and the new value would be strB =
    > '12931,12935'
    >
    > strA = '12937,'
    >
    > strB = '12931,12937,12935' (these could be any values in any order)
    >
    > I'm not sure where to begin on this.
    >
    > Thanks
    , Apr 17, 2007
    #9
    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. kaeli
    Replies:
    8
    Views:
    576
    Chris Smith
    Nov 18, 2004
  2. HS1

    Comparing two strings

    HS1, Nov 28, 2004, in forum: Java
    Replies:
    3
    Views:
    440
    Rob van der Leek
    Nov 29, 2004
  3. manzur

    comparing strings

    manzur, Mar 7, 2006, in forum: Java
    Replies:
    8
    Views:
    370
    Roedy Green
    Mar 7, 2006
  4. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    362
    Irrwahn Grausewitz
    Oct 21, 2003
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    724
    Malcolm
    Jun 24, 2006
Loading...

Share This Page