如何在C#中比较两个字符串 [英] how to compare two string in C#

查看:98
本文介绍了如何在C#中比较两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法比较两个字符串,但它会显示此字符串不同

  for  int  I =  0 ;我<  MacID.Count; I ++)
{
string t = MacID [I] .ToString();
if (t.ToString()== mac.ToString())
{
checkMAC = mac;
}
else
{
checkMAC = ;
}
}



两个字符串包含相同的值

001CCO334554

但是这个comaparison的结果返回是假的

解决方案

从你的帖子我必须做一些假设 - 见下面的代码。



正如VJ Reddy已经在解决方案#4中建议的那样,使用包含(...)。我建议也检查不区分大小写:

  1 列表< string> macIds =  new 列表< 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。包含(mac,StringComparer.InvariantCultureIgnoreCase);
14 Console.WriteLine( {0 } {1}发现,mac,found? not);



输出:

 001ccO334554 found 





干杯

Andi


如问题所述,两个字符串都包含相同内容值,似乎 MacID,mac和checkMAC 都是 string 类型。

nawazish12khan 的解决方案2运行良好,因为等于 字符串类的方法比较内容与其他参考类型的参考不同,除非它被覆盖以便以另一种方式进行比较。

但是,我认为以下代码也可用于此目的。

  string  checkMAC = MacID.Contains(mac)? mac: string  .Empty; 


既然你在谈论MAC地址,你可能会得到没有所有的字符串比较。 MAC是六个字节。其中一个非常适合 ulong 。好的,你浪费了两个字节。但是这仍然比字符串表示好很多。



通过 UInt64.TryParse()方法。比较这些是微不足道的。



为方便用户,再次显示为十六进制。 macValue.ToString(X6)应该完成这项工作

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

解决方案

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


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;


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.


这篇关于如何在C#中比较两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆