简体   繁体   中英

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

ethernetHeader.Source=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.

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. For more information, see Best Practices for Using Strings in the .NET Framework.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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