【问题标题】:Find matching string from two Arrays从两个数组中查找匹配的字符串
【发布时间】:2016-09-07 23:33:39
【问题描述】:

数组1

"ANGEL MARTIN ROMERO RIVERA"
"CRISOGONO CORTES ZARATE"
"RAFAEL ARMANDO DE LEON ALVARADO"

数组2

"ANGEL MARTIN"
"CRISOGONO ZARATE"
"RAFAEL DE LEON ALVARADO"

你能告诉我如何比较/匹配两个数组吗???

【问题讨论】:

  • edit您的帖子说明您的期望和尝试过的内容。
  • 我已经添加了 javascript 解决方案,但您需要创建一个自定义比较器。您想将“ANGEL MARTIN ROMERO RIVERA”与“ANGEL MARTIN”匹配,因此您必须手动执行此操作。这样它就不会将“ANGEL MARTIN ROMERO RIVERA”与“ANGEL CARLOS MARTIN”匹配,因此我的javascript解决方案是朝着正确方向迈出的一步......
  • 使用 C# 示例更新。

标签: c# arrays compare string-comparison


【解决方案1】:
using System;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static string[] array1 = new string[] { "ANGEL MARTIN ROMERO RIVERA", "CRISOGONO CORTES ZARATE", "RAFAEL ARMANDO DE LEON ALVARADO", "TEST" };
        static string[] array2 = new string[] { "ANGEL MARTIN", "CRISOGONO ZARATE", "RAFAEL DE LEON ALVARADO", "TEST" };

        static void Main(string[] args)
        {
            for (var i = 0; i < array1.Length; i++)
            {

                for (var j = 0; j < array2.Length; j++)
                {

                    // compare (if they will equal)
                    if (array1[i] == array2[j])
                    {
                        // will match Array1 & Array2 on "TEST", ok, so loop...
                        Console.WriteLine("Match: {0}array1:{1}{2}array2:{3}", System.Environment.NewLine, array1[i], System.Environment.NewLine, array2[j]);
                    }
                    else
                    {
                        // if data is like your example
                        if (array1[i].Contains(array2[j]))
                        {
                            Console.WriteLine("Match: {0}array1:{1}{2}array2:{3}", System.Environment.NewLine, array1[i], System.Environment.NewLine, array2[j]);
                            // match on
                            // will match Array1 & Array2 on "ANGEL MARTIN" and "TEST", ok, so loop...
                            // will NOT match on CRISOGONO ZARATE, but the else condition will...
                        }
                        else
                        {
                            // so, to get this match we do
                            var array2subArray = array2[j].Split(' ');

                            for (var k = 0; k < array2subArray.Length; k++)
                            {
                                // match all the terms in each array where the match is possible (or the smaller against the larger, i.e., if array1[i] is a name of 4 words and array2[j] is a name of 2 words, for this to be a valid match, both of the words in array2[j] must be contained in array1[i]
                                // I would write the code but then that would leave out the fun for you...

                                // Here's a start...
                                // you need to fix the below, but it is a start, blah, blah, blah...
                                if (array1[i].Contains(array2subArray[k]))
                                {
                                    // this will match on "CRISOGONO"
                                    Console.WriteLine("Potential Match: {0}array1:{1}{2}array2:{3}", System.Environment.NewLine, array1[i], System.Environment.NewLine, array2[j]);
                                    Console.WriteLine("processing furter to see if true match...");
                                }
                            }
                        }
                    }

                }
            }
            Console.ReadKey();
        }

    }
}

【讨论】:

  • 一种方法如何做到这一点,但您也可以尝试许多其他解决方案......这是一个 PURE VANILLA javascript 解决方案
  • @AlexeiLevenkov 这真是一个低质量的问题,有时我真的怀疑我们是否应该回答这些问题。
  • @mok 哇!我修好了!
【解决方案2】:

在这种情况下,两个数组的大小应该相等

此代码可能对您有用

public bool Compare(string[] arr1,string[] arr2)
{
   bool result=true;
   for(int i=0;i<arr1.length;i++)
   {
     if(arr1[i].ToLowwer()!=arr2[i].ToLowwer())
     {
       result=false;
       break;
     }
   }
   return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2021-10-10
    • 2014-02-11
    相关资源
    最近更新 更多