3

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);
    }
}
Impostor
  • 2,080
  • 22
  • 43
  • 1
    That's what `==` operator is for, yes. :) Also, you can simply concat the list into a string and do a single comparison, if you are not interested in the actual index: `string.Concat(cube) == cubeinline`. – vgru Jul 26 '16 at 12:25
  • Am I wrong in assuming this should be closed as simple typographical error? – juharr Jul 26 '16 at 12:53

5 Answers5

10

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

AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35
1

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);
MichaelThePotato
  • 1,523
  • 2
  • 10
  • 19
  • 3
    [`String.Concat`](https://msdn.microsoft.com/en-us/library/dd784338(v=vs.110).aspx) might be more appropriate here. – vgru Jul 26 '16 at 12:29
  • True, I'm just used to string.Join and have never used string.Concat. Good to know! thanks for the tip – MichaelThePotato Jul 26 '16 at 12:32
1

I use LINQ for this purpose usually. In this approach you are using the SequenceEqual method which checks two sequences (one is cube and one is Splitted string into 5 size) and checks whether two sequences are equal by comparing the elements or not:

bool res = cube.SequenceEqual(Enumerable.Range(0, cubeinline.Length / 5)
    .Select(i => cubeinline.Substring(i * 5, 5)));
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • It make sense to explain how does it works. To me it looks pretty in-efficient (`Range()` allocate memory, right?). – Sinatr Jul 26 '16 at 12:37
  • @Sinatr I'd say the inefficiency is with all the temporary sub-strings that it creates. – juharr Jul 26 '16 at 12:41
  • @Sinatr It just another option that OP can use which checks two sequences (one is `cube` and one is Splitted a string into 5 size) are equal by comparing the elements or not. – Salah Akbari Jul 26 '16 at 12:41
  • 1
    @Sinatr For the example given the amount of memory used and the time to run both isn't going to be much of a difference. You'd have to have a large string and test both to see what type of memory and runtime differences you'd have. – juharr Jul 26 '16 at 12:52
0

Change your if condition to

if ( cubeinline[i] != cube[i / maxLength][i % maxLength] )
{
    Console.WriteLine ("Error in char" + i);
}

OR please add else condition,

if ( cubeinline[i] == cube[i / maxLength][i % maxLength] )
{
    Console.WriteLine ("Match found at " + i);
}
else
{
    Console.WriteLine ("Error in char" + i);
}
A.B.
  • 1,554
  • 1
  • 14
  • 21
  • Yes Sinatr, you are right. In case of match it would add lot of noise to program output. But this if/else solution is to give an insight to OP that first if condition is intended for match (not actually for mismatch). – A.B. Jul 26 '16 at 13:15
0

What is the reasoning for storing the strings in a list? Even if you must keep them in the list, you can use a string temp variable to combine the strings in the list into a single string, then use the String.Equals method to compare your string and the temp string.

This method is better for comparing strings based on values as == checks reference equality. Heres another question you should check out.

Community
  • 1
  • 1
Pseudo Sudo
  • 1,402
  • 3
  • 16
  • 34
  • `==` in this case is not checking reference equality because they are comparing `char` (a struct), but even with `string`, a reference type, it still compares the value and not the reference because the operator is overloaded. You just have to make sure type being referenced has it overloaded if you want value equality instead of reference (but most .Net classes that you would want to do value comparisons on do overload it). – juharr Jul 26 '16 at 12:44