Comparing Strings

S

shankwheat

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
 
D

Daz

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.
 
L

Lee

Daz said:
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.


--
 
D

Daz

Daz said:








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.
 
E

Evertjan.

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>
=================================
 
D

Daz

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



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

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.
 
S

shankwheat

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!
 
L

Lee

Daz said:
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.


--
 
G

gldunne

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));
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top