Click here to Skip to main content
12,399,299 members (60,214 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET
I have used the following method to compare two string but it will show this string are different
for (int I = 0; I < MacID.Count; I++)
    {
        string t = MacID[I].ToString();
        if (t.ToString()==mac.ToString())
        {
            checkMAC = mac;
        }
        else
        {
            checkMAC = "";
        }
    }
both string contains same value
"001CCO334554"
but result return by this comaparison is false
Posted 8-Apr-12 19:44pm
Updated 8-Apr-12 19:48pm
v3
Comments
Philippe Mori 9-Jul-15 12:16pm
   
If both strings are the same thant the result would be true. If it returns false, then it must be different. Maybe a mix of uppercase and lowercase... In fact, it is easy to see that in your string you mix digit 0 and letter O thus it is not surprising that it does not works.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

From your post I must make some assumptions - see code below.

As VJ Reddy already suggests in Solution #4, use Contains(...). I would suggest to also check case insensitive:
  1  List<string> macIds = new List<string>
  2  {
  3      "001CCO334550",
  4      "001CCO334551",
  5      "001CCO334552",
  6      "001CCO334553",
  7      "001CCO334554",
  8      "001CCO334555",
  9      "001CCO334556",
 10  };
 11  
 12  string mac = "001ccO334554";
 13  bool found = macIds.Contains(mac, StringComparer.InvariantCultureIgnoreCase);
 14  Console.WriteLine("{0} {1}found", mac, found ? "" : "not ");
Output:
001ccO334554 found

Cheers
Andi
  Permalink  
v4
Comments
VJ Reddy 9-Apr-12 13:22pm
   
Good suggestion. +5
Andreas Gieriet 9-Apr-12 13:24pm
   
Uups! I got your name wrong in my post - Sorry! Fixed now.
Thanks for your 5!
Andi
VJ Reddy 9-Apr-12 13:30pm
   
It's OK. Thanks for the correction.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

As mentioned in the question that both strings contain same value, it appears that MacID, mac and checkMAC are all of string type.
The solution 2 by nawazish12khan works well, as Equals method of string class compares the contents unlike the reference for other reference types, unless it is overridden to compare in another way.
However, I think the following code may also serve the purpose.
string checkMAC = MacID.Contains(mac) ? mac : string.Empty;
  Permalink  
v2
Comments
Andreas Gieriet 9-Apr-12 13:26pm
   
Concise solution. My 5!
VJ Reddy 9-Apr-12 13:41pm
   
Thank you, Andreas.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

Since you're talking of MAC addresses, you could get away without all the string comparing. MACs are six bytes. One of them fits nicely in a ulong. Ok, you're wasting two bytes. But that's still a lot better than the string representation.

Get your ulongs via the UInt64.TryParse() method. Comparing those is trivial.

For user convenience, display as hexadecimal again. macValue.ToString("X6") should do the job.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

for (int I = 0; I < MacID.Count; I++)
{
    string t = MacID[I].ToString();
    if (t.Equals(mac.ToString()))    //Use t.Equals
    {
        checkMAC = mac;
    }
    else
    {
        checkMAC = "";
    }
}



Enjoy...
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

In addition to other solutions, we can clearly see that you mix letter O with digit 0. Maybe only one string has this inconsitencies and it might be the reason why they are different.

When writting code, you should use a font that allows you to see the difference between both. Usually, the letter is wider than the digit.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

use string.Compare() method
string str1= "test";
string str2= "test";
int i = string.Compare(str1, str2);
it returns int value. 0 in case true else it returns -1.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 11 Jun 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100