Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing data in different structure

Tags:

string

c#

list

I have a string

string cubeinline = "12345123451234X1234512345";

which is equal to a List<string>

List<string> cube = new List<string>(){ "12345",
                                        "12345",
                                        "1234X",
                                        "12345",
                                        "12345"};

But different arranged. The string is split by length. In this case 5.

Now i need to compare the string with the List - char by char. But my method says every char is invalid.

int maxLength = 5;
for (int i = 0; i < cubeinline.Length; i++)
{
    if (cubeinline[i] == cube[i / maxLength][i % maxLength])
    {
        Console.WriteLine("Error in char" + i);
    }
}
like image 228
Impostor Avatar asked May 02 '26 04:05

Impostor


2 Answers

Change == into !=. You inverse the logic here: the program should display the message when there is a difference, not an equlity!

like image 50
AhmadWabbi Avatar answered May 04 '26 18:05

AhmadWabbi


you can do it like this:

string cubeinline = "12345123451234X1234512345";
List<string> cube = new List<string>(){ "12345",
                                    "12345",
                                    "1234X",
                                    "12345",
                                    "12345"};
bool isEqual = cubeinline == string.Concat(cube);
like image 21
MichaelThePotato Avatar answered May 04 '26 20:05

MichaelThePotato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!