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.

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

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;
}
alc6379 commented: Great coding! +9

Awesome! Thanks for submitting!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.