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 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 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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 440
1 Sergey Alexandrovich Kryukov 221
2 OriginalGriff 183
3 Tadit Dash 160
4 Guruprasad.K.Basavaraju 125
0 Sergey Alexandrovich Kryukov 9,255
1 OriginalGriff 5,433
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 9 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid