0

So here is my code:

      if (txtboxAntwoord.Text == lblProvincie.Text)
            {

            }

The thing I want to achieve is: make the if statement so that it does check if the text is the same, but it does not check if the text contains upper- or lowercases.

Let's say lblProvincie's text = "Some Text" and I want to check if the containing text of txtboxAntwoord is the same, but it shouldn't matter if it contains the uppercases of the text.

Gekkehond
  • 128
  • 1
  • 13
  • Look at [string.Compare](https://msdn.microsoft.com/en-us/library/zkcaxw5y(v=vs.110).aspx) overloads that consider the case and for a summary of the various compare methods [Differences in string compare methods in C#](http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp) – Steve Oct 01 '16 at 16:52

3 Answers3

2

You can use the .Equals method on string and pass in a string comparison option that ignores case.

if (string.Equals(txtboxAntwoord.Text, lblProvincie.Text, 
               StringComparison.OrdinalIgnoreCase))

for pure speed where culture-based comparison is unimportant

OR

if (string.Equals(txtboxAntwoord.Text, lblProvincie.Text, 
               StringComparison.CurrentCultureIgnoreCase))

if you need to take culture-based comparisons into account.


While this approach may be slightly more complicated, it is more efficient than the ToUpper() approach since new strings do not need to be allocated. It also has the advantage of being able to specify different comparison options such as CurrentCultureIgnoreCase.

While this may not be much of an impact on application performance in an isolated context, this will certainly make a difference when doing large amounts of string comparisons.

const string test1 = "Test1";
const string test2 = "test1";

var s1 = new Stopwatch();
s1.Start();

for (int i = 0; i < 1000000; i++)
{
    if (!(test1.ToUpper() == test2.ToUpper()))
    {
        var x = "1";
    }
}
s1.Stop();
s1.ElapsedMilliseconds.Dump();

var s2 = new Stopwatch();
s2.Start();
for (int i = 0; i < 1000000; i++)
{
    if(!string.Equals(test1, test2,
           StringComparison.OrdinalIgnoreCase))
    {
        var x = "1";
    }
}
s2.Stop();
s2.ElapsedMilliseconds.Dump();

The first contrived example takes 265 milliseconds on my machine for 1 million iterations. The second only takes 25. In addition, there was additional string creation for each of those iterations.

Per Mike's suggestion in the comments, it is only fair to also profile CurrentCultureIgnoreCase. This is still more efficient than ToUpper, taking 114 milliseconds which is still over twice as fast as ToUpper and does not allocate additional strings.

David L
  • 32,885
  • 8
  • 62
  • 93
  • This will also do the trick, but ToUpper is easier to use. Thanks anyways. – Gekkehond Oct 01 '16 at 16:54
  • @Tuur correct, it is easier to use, but it is less efficient. Please see update. – David L Oct 01 '16 at 16:56
  • Since you went into the trouble of coding all this, you might want to add one more check with `StringComparison.CurrentCultureIgnoreCase`. I am sure it would perform worse, I wonder by how much. – Mike Nakis Oct 01 '16 at 17:00
  • but we are not comparing million rows in a loop, so it does not make any difference if it is needed to be done once, but yes if we are processing a lot of rows in a loop then string.Equals should be used – Ehsan Sajjad Oct 01 '16 at 17:01
  • @EhsanSajjad in this case, absolutely, but it is also a good habit to get into. Creating bad habits in one-off scenarios leads to polluted code. I think it is better to choose the approach that you would consistently stick with over time rather than one-off solutions with rules that can only apply where performance isn't an issue. – David L Oct 01 '16 at 17:03
  • @EhsanSajjad we have absolutely no information as to whether we are comparing millions of rows in a loop or not. Therefore, it is best to take performance into account. Especially when garbage collection is involved. – Mike Nakis Oct 01 '16 at 17:03
  • well, for this case it is clear we are reading textbox value, it would be of no use to read same checkbox in a loop – Ehsan Sajjad Oct 01 '16 at 17:03
1

You can use ToUpper() or ToLower on both values so that both have same case uppor or lower, you can do it like:

if (txtboxAntwoord.Text.ToUpper() == lblProvincie.Text.ToUpper())
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

What you are looking for is called "case insensitive string comparison".

You can achieve it with Ehsan Sajjad's suggestion, but it would be inefficient, because for each comparison you would be generating at least one (in his example two, but that can be optimized) new string to contain the uppercase version of the string to compare to, and then immediately letting that string be garbage-collected.

David L's suggestion is bound to perform a lot better, though I would advise against StringComparison.OrdinalIgnoreCase, because it ignores the current culture.

Instead, use the following:

string.Equals( text1, text2, StringComparison.CurrentCultureIgnoreCase )
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142