简体   繁体   English

C#Bool变量始终为false

[英]C# Bool Variable always false

I am comparing two strings: 我正在比较两个字符串:

bool d = 
    (String.Equals(ethernetHeader.Source,staticForm.textBox1.Text.ToString()));

this statement is always false even in console both are same as below.. 即使在控制台中都与以下相同,此语句也始终为false。

ethernetHeader.Source=00:25:64:4F:21:D9 ethernetHeader.Source = 00:25:64:4F:21:D9

textBox1.Text=00:25:64:4F:21:D9 textBox1.Text = 00:25:64:4F:21:D9

any possible reason?? 任何可能的原因?

thanks, 谢谢,

使用Trim以便在字符串的开头或结尾没有空格。

Boolean d = ethernetHeader.Source.Trim() == staticForm.textBox1.Text.Trim();

Use an override with StringComparison. 对StringComparison使用替代。

When you call a string comparison method such as String.Compare, String.Equals, or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. 当您调用字符串比较方法(例如String.Compare,String.Equals或String.IndexOf)时,应始终调用包含StringComparison类型参数的重载,以便您可以指定该方法执行的比较类型。 For more information, see Best Practices for Using Strings in the .NET Framework. 有关更多信息,请参见.NET Framework中使用字符串的最佳实践。

http://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx

bool d = 
    (String.Equals(ethernetHeader.Source, staticForm.textBox1.Text.ToString(), StringComparison.OrdinalIgnoreCase));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM