| | |
Array/String intersect and Array/Array intersect code
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
got to thinking that i couldnt find a way to find the intersect of a string and array nor could i find a quick way to do an array / array.
i searched a bit on google and didnt really find anything that showed me something like
Array.Intersect(array a, array b)
so i wrote my own
i hope this helps someone out there. even if there is a faster way it is nice to discover how things were/are done.
array vs string intersect.
array vs array intersect
i searched a bit on google and didnt really find anything that showed me something like
Array.Intersect(array a, array b)
so i wrote my own
i hope this helps someone out there. even if there is a faster way it is nice to discover how things were/are done.
array vs string intersect.
C# Syntax (Toggle Plain Text)
public static string Intersect(string[] arrayA,string intersectObject) { int arrayALength = arrayA.Length; int count = 0, maxCount = arrayALength; string strOutput = ""; //this is only going to find one intersection. thats all i really give a hoot about while(count >= 0 && count < maxCount) { if(arrayA[count] == null) { //if it finds a null value arrayA[count] = "";//insert a blank space } if(arrayA[count].Equals(intersectObject)) { //if it finds an intersection strOutput = intersectObject; } count++; } //return the results if(strOutput != intersectObject) { strOutput = "false"; } return strOutput; }
array vs array intersect
C# Syntax (Toggle Plain Text)
public static string[] arrayIntersect(string[] arrayA, string[] arrayB)//returns ONLY the first found strings in common between array A and array B { //count the length of both arrays int intArrayA = arrayA.Length, intArrayB = arrayB.Length; //Find Max array size if all vallues from shortest match all the values from the biggest int intMaxOutput = 0; string[] longArray, shortArray; if (intArrayA >= intArrayB) { intMaxOutput = intArrayB; shortArray = new string[intArrayB];//declare its size shortArray = arrayB;//transfer all value longArray = new string[intArrayA];//declare its size longArray = arrayA;//transfer all values } else//intArrayB is bigger { intMaxOutput = intArrayA; shortArray = new string[intArrayA];//declare its size shortArray = arrayA;//transfer all value longArray = new string[intArrayB];//declare its size longArray = arrayB;//transfer all values } //array for storing the strings string[] strArrayOutput = new string[intMaxOutput]; //If all possible values matches then the shortest array will determin the max number of matches. int shortCount = 0;//count through short array int longCount = 0;//count through long array int intOutput = 0;//starts the counter for each value that could be found /* * !!!side not to self!!! * since we know which of the arrays is the smallest * we only need to count through the while statement as many times * as the shortest array length, testing it each time against the other array * each time a value is found it needs to be stored into the array output * */ //take care of any null values in the short array while (shortCount >= 0 && shortCount < shortArray.Length) { if(shortArray[shortCount] == null) { shortArray[shortCount] = ""; shortCount++; } else { shortCount++; } } //take care of any null values in the long array while (longCount >= 0 && longCount < longArray.Length) { if(longArray[longCount] == null) { longArray[longCount] = ""; longCount++; } else { longCount++; } } //reset counters longCount = 0; shortCount = 0; //begin array checking while(shortCount >= 0 && shortCount < intMaxOutput) { //If longCount is at max but shortCount is not if(longCount >= (longArray.Length - 1) && shortCount != (shortArray.Length - 1)) { //test to make sure these values do not match if(shortArray[shortCount] != longArray[longCount]) { //we are still testing values longCount = 0;//reset the long counter shortCount++;//update the short counter } else { //something matches strArrayOutput[intOutput] = shortArray[shortCount]; intOutput++;//incriment the output array longCount = 0;//reset the long counter shortCount++;//update the short counter } } if(shortCount >= (shortArray.Length -1) && longCount >= (longArray.Length -1)) { //we have left the boudns of this array //do nothing shortCount++;//this will end the while statment } else { //test for match if(shortArray[shortCount] != longArray[longCount]) { //if the two array strings do not match longCount++; //increase the count of the longer array } else { //if the two array strings do match strArrayOutput[intOutput] = shortArray[shortCount];//assign the value to the return array intOutput++;//incriment the output array shortCount++;//incriment the A counter longCount = 0;//reset the B counter } } } return strArrayOutput; }
Dont forget to spread the reputation to those that deserve!
![]() |
Similar Threads
- I still need help with my program!! (Java)
- counting strings (C)
- Array String not working? (JSP)
- Conver int Array into a String (Java)
- new and in need (ASP)
- Array to String (PHP)
- string to integer array transformation (C)
Other Threads in the C# Forum
- Previous Thread: Thread pausing and shared variable synchronization?
- Next Thread: need to make setup with msde
| Thread Tools | Search this Thread |
.net access algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client clock combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees developer development draganddrop drawing dynamiccreation encryption enum excel file form format forms function gdi+ hospitalmanagementinformationsystem hospitalmanagementsystems image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql operator password path photoshop picturebox pixelinversion post priviallages. programming property radians regex remoting richtextbox running... serialization server sleep soap socket sql sqlserver stack statistics stream string table temperature text textbox thread time timer update usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf write xml






