简体   繁体   English

比较两个字符串数组

[英]Compare two Strings Arrays

I have to compare those two strings but i have no idea how. 我必须比较这两个字符串,但我不知道如何。 I've tryed everything i have found in the web but nothing works. 我已经尝试了在网上找到的所有内容,但没有任何效果。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aufgabe_3_2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] zeichenSatz = new string[,] { {" ","0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"},          //creates a string array
                                                    { "0010","SP","!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/"},
                                                    { "0011","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?"},
                                                    { "0100","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"},
                                                    { "0101","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_"},
                                                    { "0110","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"},
                                                    { "0111","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~",""},
                                                    { "1010","NBSP","¡­","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","SHY","®","¯"},
                                                    { "1011","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿"},
                                                    { "1100","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï"},
                                                    { "1101","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß"},
                                                    { "1110","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï"},
                                                    { "1111","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ"}};

            Console.WriteLine("Bitte geben sie ihren Text ein: ");                 //request a input from user
            string strText = Console.ReadLine();
            string[] strEingabe;                                 //makes other stuff
            strEingabe = strText.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
            string[] tempArray = new string[strText.Length];
            Console.WriteLine(strEingabe);
            Console.WriteLine(strText);
            Console.WriteLine(strText.Length);
            Console.Write("0100||");

            for (int h = 0; h < strText.Length; h++)
            {
                for (int i = 0; i < zeichenSatz.GetLength(0); i++)
                {
                    for (int j = 0; j < zeichenSatz.GetLength(1); j++)
                    {
                        if (strText[h].Equals(zeichenSatz[i, j]) == true)
                        {
                            Console.WriteLine("{0} , {1}", zeichenSatz[i, 0], zeichenSatz[0, j]);
                            Console.WriteLine(zeichenSatz[0, j]);
                            Console.WriteLine(" ");
                            }
                        }
                    }
                }
                Console.WriteLine("0000");
                Console.ReadLine();
                }
            }
        }

We have made an extension over the IEnumerable. 我们对IEnumerable进行了扩展。

In our case, we want to know if all items of the collection A is in the collection B, not necessarly in the same order, I don't know if it's what you want: 在我们的情况下,我们想知道集合A的所有项目是否都在集合B中,而不必按相同的顺序进行,我不知道这是否是您想要的:

public static bool ContentEquals<T>(this IEnumerable<T> enumerable, IEnumerable<T> other)
    {
        if (Equals(enumerable, other))
        {
            return true;
        }

        if (enumerable == null || other == null)
        {
            return false;
        }

        if (enumerable.Count() != other.Count() || enumerable.Except(other).Any())
        {
            return false;
        }

        foreach (T value in enumerable.Distinct())
        {
            if (enumerable.Count(v => Equals(v, value)) != other.Count(v => Equals(v, value)))
            {
                return false;
            }
        }

        return true;
    }

usage: 用法:

if(listOne.ContentEquals(listTwo)){
   ...
}

In your case, I guess you can do: zeichenSatz[0].ContentEquals(zeichenSatz[1]); 就您而言,我想您可以做到: zeichenSatz[0].ContentEquals(zeichenSatz[1]);

您可以使用Linq:

bool areEqual = a.SequenceEqual(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM