比较两个字符串数组

时间:2016-04-12 09:41:01

标签: c# arrays string compare equals

我必须比较这两个字符串,但我不知道如何。我尝试过在网络上找到的所有内容,但没有任何作用。

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

2 个答案:

答案 0 :(得分:0)

我们对IEnumerable进行了扩展。

在我们的案例中,我们想知道集合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;
    }

<强>用法:

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

在您的情况下,我猜您可以这样做:zeichenSatz[0].ContentEquals(zeichenSatz[1]);

答案 1 :(得分:0)

您可以使用Linq:

bool areEqual = a.SequenceEqual(b);