Recipe 5.9. Comparing Strings Without Case Sensitivity


Problem

You need to compare two strings without regard to their case.

Solution

Use the shared Compare() method provided by the String object to compare two strings:

 Select Case String.Compare(content1, content2, True)    Case Is < 0       MsgBox("Content1 comes before Content2.")    Case Is > 0       MsgBox("Content1 comes after Content2.")    Case Is = 0       MsgBox("Content1 and Content2 are the same.") End Select 

Setting the third parameter of the Compare() method to True instructs the method to perform a case-insensitive comparison.

Discussion

This type of string comparison compares all alphabetic characters as though lower-case and uppercase characters were identical. Figure 5-7 shows that "apples" is equal to "Apples" when the strings are compared this way.

Figure 5-7. When case is ignored, lowercase and uppercase are treated identically


String comparisons are culturally defined by default, so be sure the sort order you get is really what you want. See the Visual Studio online help for the CompareOptions property to find more information on how to make changes to the way strings are sorted.

See Also

Recipe 5.8 discusses related comparisons.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net