Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Comparing Strings

Reply
Thread Tools

Comparing Strings

 
 
shankwheat
Guest
Posts: n/a
 
      04-16-2007
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

 
Reply With Quote
 
 
 
 
Daz
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.com> 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.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      04-16-2007
Daz said:
>
>On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.com> 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.


--

 
Reply With Quote
 
Daz
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 5:30 pm, Lee <REM0VElbspamt...@cox.net> wrote:
> Daz said:
>
>
>
>
>
> >On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.com> 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.

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      04-16-2007
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)
 
Reply With Quote
 
Daz
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 5:52 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> 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.

 
Reply With Quote
 
shankwheat
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 12:56 pm, "Daz" <cutenfu...@gmail.com> wrote:
> On Apr 16, 5:52 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> 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!

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      04-16-2007
Daz said:
>
>On Apr 16, 5:30 pm, Lee <REM0VElbspamt...@cox.net> wrote:
>> Daz said:
>>
>>
>>
>>
>>
>> >On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.com> 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.


--

 
Reply With Quote
 
gldunne@gmail.com
Guest
Posts: n/a
 
      04-17-2007
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" <evanbu...@gmail.com> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
comparing strings manzur Java 8 03-07-2006 05:05 PM
Comparing two strings HS1 Java 3 11-29-2004 04:21 PM
Which way is more efficient - comparing strings of different letter casing kaeli Java 8 11-18-2004 09:44 PM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57