Tech Blog‎ > ‎

VB.NET - Compare Strings - Case Sensitivity Issues

posted Apr 21, 2011, 9:30 AM by Victor Zakharov
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:

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.