Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used the following method to compare two string but it will show this string are different
C#
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
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.

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:
C#
  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
 
Share this answer
 
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.
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.
C#
string checkMAC = MacID.Contains(mac) ? mac : string.Empty;
 
Share this answer
 
v2
Comments
Andreas Gieriet 9-Apr-12 13:26pm    
Concise solution. My 5!
VJ Reddy 9-Apr-12 13:41pm    
Thank you, Andreas.
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.
 
Share this answer
 
 
Share this answer
 
C#
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...
 
Share this answer
 
v2
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.
 
Share this answer
 
use string.Compare() method
C#
string str1= "test";
string str2= "test";
int i = string.Compare(str1, str2);

it returns int value. 0 in case true else it returns -1.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900