Click here to Skip to main content
Rate this: bad
good
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
Edited 8-Apr-12 19:48pm
v3
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 at 9-Apr-12 13:26pm
   
Concise solution. My 5!
VJ Reddy at 9-Apr-12 13:41pm
   
Thank you, Andreas.
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 at 9-Apr-12 13:22pm
   
Good suggestion. +5
Andreas Gieriet at 9-Apr-12 13:24pm
   
Uups! I got your name wrong in my post - Sorry! Fixed now.
Thanks for your 5!
Andi
VJ Reddy at 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 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

please show the MacID array values
  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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 447
1 CPallini 355
2 OriginalGriff 245
3 George Jonsson 159
4 Abdul Samad KP 135
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,125
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 Apr 2012
Copyright © CodeProject, 1999-2014
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