Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
Can any one please tell me what is the best way to compare two strings in C#, the result of comparision must be true or false.
Actually i am using
((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[IDIndexRow, ObjTypeCol]).Value2.ToString)) == "ID")
But this doesnt seems to work. it seems like there is some problem with conversion.
Posted
Updated 29-Jun-10 18:02pm
v2

you can use any of the below methods.
string s1="abc";
string s2="abc";

//using if else
if(s1 == s2)
return true;
else

return false;

//using ternary operator
bool cmp = ((s1==s2)?true:false);
 
Share this answer
 
Comments
asjadazeez 30-Jun-10 0:04am    
Actually i am using (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[IDIndexRow, ObjTypeCol]).Value2.ToString) == "ID") but some type conversion problem can you please hemp me out.
Hi,

string.equals(string) should do the trick and is the preferred way to go

string str = "Hello";
string str2 = "Hello";
bool bResult = str.Equals(str2);
 
Share this answer
 
Comments
asjadazeez 30-Jun-10 0:04am    
Actually i am using (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[IDIndexRow, ObjTypeCol]).Value2.ToString) == "ID") but some type conversion problem can you please hemp me out.
asjadazeez 30-Jun-10 0:15am    
OK this works fine.
From what I know, have tried, and have read around the internets

String.Equals(string) [The standard String.Equals]

and

==

Are essentially the same thing. I prefer to use the following in a lot of cases though (if I'm not sure and don't want to mess around with case);

string.Equals( string, StringComparison.CurrentCultureIgnoreCase )
 
Share this answer
 
Comments
asjadazeez 30-Jun-10 0:15am    
Thanks for additional info.!

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