rkt_167
rkt_167

Reputation: 7

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,

Upvotes: 0

Views: 246

Answers (2)

Edwin van Vliet
Edwin van Vliet

Reputation: 567

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

Upvotes: 0

Xaruth
Xaruth

Reputation: 4114

Use Trim in order to have no spaces on lead or end of your string.

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

Upvotes: 1

Related Questions