473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Comparing Strings

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 16 '07 #1
8 1553
Daz
On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.comwrote:
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.

Apr 16 '07 #2
Lee
Daz said:
>
On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.comwrote:
>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.
--

Apr 16 '07 #3
Daz
On Apr 16, 5:30 pm, Lee <REM0VElbspamt...@cox.netwrote:
Daz said:


On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.comwrote:
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.

Apr 16 '07 #4
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)
Apr 16 '07 #5
Daz
On Apr 16, 5:52 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
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.

Apr 16 '07 #6
On Apr 16, 12:56 pm, "Daz" <cutenfu...@gmail.comwrote:
On Apr 16, 5:52 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:


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!

Apr 16 '07 #7
Lee
Daz said:
>
On Apr 16, 5:30 pm, Lee <REM0VElbspamt...@cox.netwrote:
>Daz said:


>On Apr 16, 5:08 pm, "shankwheat" <evanbu...@gmail.comwrote:
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.
--

Apr 16 '07 #8
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.comwrote:
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 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
15
by: luc.saffre | last post by:
Hello, here is something that surprises me. #coding: iso-8859-1 s1=u"Frau Müller machte große Augen" s2="Frau Müller machte große Augen" if s1 == s2: pass
1
by: Jetboy555 | last post by:
Sample input: 2000 Georgia Tech 30 Virginia 20 1999 Virginia 20 Virginia tech My Problem is in taking the input in correctly. I take the year in correctly, but i'm having trouble with the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.