This page looks best with JavaScript enabled

VB.NET - Compare strings - case sensitivity issues

 ·   ·  ☕ 1 min read

I recently stumbled upon this problem in one of our company .NET projects.
For some reason, project settings there were configured for Text comparison, rather than Binary.

As a result, lower case strings were equal to upper case, which was discovered later when some bug in UI occured.

There were three solutions available:

  1. Switch default comparison type on the project level. Go to Project / Properties / Option Compare -> Binary.

  2. Set default comparison type on the document level. Put Option Compare Text or Option Compare Binary on top of the code, where applicable.

  3. Change to String.Compare(strA As String, strB As String, ignoreCase As Boolean) on a per-use basis:

    1
    2
    
    String.Compare(str1, str2, False) 'case sensitive
    String.Compare(str1, str2, True) 'case insensisitve
    

We went with (1), but for some cases (2) and (3) might be better, especially if you don’t want to break your existing code.


Victor Zakharov
WRITTEN BY
Victor Zakharov
Web Developer (Angular/.NET)