如何在C#中比较两个字符串 [英] How to compare two strings in C#
本文介绍了如何在C#中比较两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
谁能告诉我在C#中比较两个字符串的最佳方法是什么,比较的结果必须为true或false.
实际上我正在使用
(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells [IDIndexRow,ObjTypeCol]).Value2.ToString))=="ID")
但这似乎不起作用.似乎转换存在问题.
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.
推荐答案
您可以使用以下任何一种方法.
字符串s1 ="abc";
字符串s2 ="abc";
//使用if else
if(s1 == s2)
返回true;
其他
返回false;
//使用三元运算符
bool cmp =((s1 == s2)?true:false);
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);
string.equals(string)应该可以解决问题,并且是首选的方法
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);
据我所知,已经尝试并阅读了互联网
String.Equals(string)[标准的String.Equals]
和
==
本质上是同一回事.我更喜欢在很多情况下使用以下命令(如果我不确定并且不想弄乱情况);
string.Equals(string,StringComparison.CurrentCultureIgnoreCase)
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 )
这篇关于如何在C#中比较两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文