I have three string arrays
string[] stringArray= {"AAA", "BBB", "CCC","DDD", "EEE","FFF","GGG","HHH" };
string[] stringArray1 = { "A", "B", "C","D", "E","F","G","H" };
string[] stringArray2 = { "BBB", "DDD","FFF","HHH" };
How could I compare stringArray2
with stringArray
and find the index number stringArray
that matches. After finding the index ,I need to apply it to stringArray1
and display the results.
I have tried the following code ,but failed to get the match string
int j=0;
string line = null;
List<string> finalstring = new List<string>();
for (int i = 0; i < stringArray.Count; i++)
{
while(stringArray[i] == stringArray2[j])
{
line = stringArray2[i];
finalstring.Add(line);
j++;
}
}
I think you have to try like this:
var result = stringArray1.Where(c => // iterating stringArray1
stringArray2.Where(x => stringArray.Contains(x)) // filtering stringArray2 elements
.Any(y=>y.Contains(c))).ToList(); // collect the final result
A Working example for your reference
int j=0;
string line = null;
List<string> finalstring = new List<string>();
for (int i = 0; i < stringArray.Length; i++)
//for (int i = 0; i < stringArray.Count; i++) // <-- Count is not proper way to get total elements in an array
{
j = 0; // <-- you failed to reinitialize 'j'
//while(stringArray[i] == stringArray2[j]) // <-- while is not proper way to compare
if(stringArray[i] == stringArray2[j])
{
//line = stringArray2[i];
//if only first charracter is needed
finalstring.Add(new string(stringArray2[i][0], 1));
//if complete string is needed
//finalstring.Add(stringArray2[i]);
j++;
}
}
Above is not an optimized way to do the comparisons. You can try using HashSet class
string[] stringArray= {"AAA", "BBB", "CCC","DDD", "EEE","FFF","GGG","HHH" };
string[] stringArray1 = { "A", "B", "C","D", "E","F","G","H" };
string[] stringArray2 = { "BBB", "DDD","FFF","HHH" };
//convert string array to hashset
var hashSet = new HashSet<string>(stringArray);
int j=0;
string line = null;
List<string> finalstring = new List<string>();
for (int i = 0; i < stringArray2.Length; i++)
{
if(hashSet.Contains(stringArray2[i]))
{
//if only first charracter is needed
finalstring.Add(new string(stringArray2[i][0], 1));
}
}
List<string> finalList = new List<string>();
stringArray.Select((value,index) => new { value, index })
.Where(num => stringArray2.Contains(num.value)).ToList()
.ForEach(num => finalList.Add(stringArray1[num.index]));
You are trying to compare the elements on same index but you have to search the element of stringArray2
in stringArray
. You can use Array.IndexOf to find in Array the required element.
string[] stringArray= {"AAA", "BBB", "CCC","DDD", "EEE","FFF","GGG","HHH" };
string[] stringArray1 = { "A", "B", "C","D", "E","F","G","H" };
string[] stringArray2 = { "BBB", "DDD","FFF","HHH" };
int j=0;
string line = null;
List<string> finalstring = new List<string>();
for (int i = 0; i < stringArray2.Count(); i++)
{
if(Array.IndexOf(stringArray, stringArray2[i]) != -1)
{
int idx = Array.IndexOf(stringArray1, stringArray2[i][0].ToString());
if(idx!=-1)
finalstring.Add(stringArray1[idx]);
}
}
Edit using Linq
var result2 = stringArray1.Where(c=>
stringArray2.Intersect(stringArray)
.Any(i=>i.Contains(c)));
you could do it with selecting the index via Linq ( where >= 0
filters out all non matches):
string[] stringArray = { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH" };
string[] stringArray1 = { "A", "B", "C", "D", "E", "F", "G", "H" };
string[] stringArray2 = { "BBB", "DDD", "FFF", "HHH" };
var matches = stringArray2
.Select(s => Array.FindIndex(stringArray, s2 => s2 == s))
.Where(i => i >= 0).Select(i => stringArray1[i])
.ToList();
Here a dotnetfiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.