如何比较两个字符串数组并找到公共字符串数组的索引号并将其应用于第三个字符串数组?

时间:2017-01-17 05:57:51

标签: c# arrays string

我有三个字符串数组

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" };

如何将stringArray2stringArray进行比较,并找到匹配的索引号stringArray。 找到索引后,我需要将其应用于stringArray1 并显示结果。

我尝试了以下代码,但未能获得匹配字符串

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++;
   }
}

5 个答案:

答案 0 :(得分:1)

我想你必须尝试这样:

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

Working example供您参考

答案 1 :(得分:1)

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++;
   }
}

以上不是进行比较的优化方法。您可以尝试使用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));
        }
    } 

答案 2 :(得分: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]));

答案 3 :(得分:0)

您正在尝试比较同一索引上的元素,但您必须在stringArray2中搜索stringArray的元素。您可以使用Array.IndexOf在Array中查找所需的元素。

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]);
    }  
}
使用Linq

修改

var result2 = stringArray1.Where(c=>
              stringArray2.Intersect(stringArray)
              .Any(i=>i.Contains(c)));

答案 4 :(得分:0)

你可以通过Linq选择索引来做到这一点(where >= 0过滤掉所有非匹配项):

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

这是dotnetfiddle