2016-09-07 5 views
-2

하는 array1두 배열

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

배열 2에서 일치하는 문자열을 찾기

내가 두 배열과 일치/비교하는 방법을
"ANGEL MARTIN" 
"CRISOGONO ZARATE" 
"RAFAEL DE LEON ALVARADO" 

당신이 말해 주시겠습니까 ???

+1

[편집] 게시물이 명확하게하십시오 당신이 기대 당신이했습니다 시도했다. –

+0

javascript 솔루션을 추가했지만 사용자 지정 비교자를 만들어야합니다. "ANGEL MARTIN ROMERO RIVERA"를 "ANGEL MARTIN"과 일치 시키려면 수동으로해야합니다. 그래서 "ANGEL MARTIN ROMERO RIVERA"와 "ANGEL CARLOS MARTIN"이 일치하지 않으므로 자바 스크립트 솔루션은 올바른 방향으로 나아가는 단계입니다 ... –

+0

C# 예제로 업데이트되었습니다. –

답변

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

    } 
} 
+0

이 방법은 한 가지 방법이지만 다른 솔루션도 다양하게 시도 할 수 있습니다. 여기 순수 퓨전 자바 스크립트 솔루션입니다. –

+0

@AlexeiLevenkov 정말 품질이 낮은 질문이며 때로는 이러한 질문에 대답해야하는지 여부도 의심 스럽습니다. 전혀 또는 아닙니다. – mok

+0

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