O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required