Perform Case-Insensitive String Comparisons

Problem

You need to compare two strings to see if they match, even if the capitalization differs.

Solution

Use the overloaded version of the shared String.Compare method that accepts the ignoreCase Boolean parameter, and set ignoreCase to True.

Discussion

The String.Compare method accepts two strings and returns 0 if the strings are equal, -1 if the first string is less than the second (StringA < StringB), or 1 if the first string is greater than the second (StringA > StringB). Optionally, the Compare method can also accept a Boolean parameter called ignoreCase. Set the ignoreCase parameter to True to perform a case-insensitive comparison.

If String.Compare(StringA, StringB, true) = 0 Then ' Strings match ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.