Comparing Strings in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the methods for comparing the values of strings.

The most common way to compare two strings is the following:

                    
                        Dim MyStringA As String = "Hello World!"
                        Dim MyStringB As String = "HELLO WORLD!"      
                        If MyStringA = MyStringB Then
                            ' Do something
                        Else
                            ' Do something else
                        End If              
                    
However in some situations this approach is not efficient because it offer no options for case and cultural variance like the other .NET built-in value-comparison methods.

1. String.Compare: compares two strings and returns integer as result indicating their position in sort order this method is culturally aware and has overloads that regard or disregard case-sensitivity and cultural difference. The result as mentioned is an integer and can be:

negative number (MyStringA is less than MyStringB)
zero (MyStringA equals MyStringB)
positive number (MyStringA is greater than MyStringB) or 1 if Value is Nothing

                    
                        Dim MyStringA As String = "Hello World!"
                        Dim MyStringB As String = "HELLO WORLD!"      
                        If String.Compare(MyStringA, MyStringB) = 0 Then
                            ' Do something
                        Else
                            ' Do something else
                        End If              
                    


2. String.CompareOrdinal: compares two strings but without considering the local culture.
this method is mainly used for strings ordering and sorting.
                    
                        Dim MyStringA As String = "Hello World!"
                        Dim MyStringB As String = "HELLO WORLD!"      
                        If String.CompareOrdinal(MyStringA, MyStringB) = 0 Then
                            ' Do something
                        Else
                            ' Do something else
                        End If              
                    


3. String.CompareTo: compares a string that encapsulates to other string.
this method is also mainly used for strings ordering and sorting.

                    
                        Dim MyStringA As String = "Hello World!"
                        Dim MyStringB As String = "HELLO WORLD!"      
                        If MyStringA.CompareTo(MyStringB) = 0 Then
                            ' Do something
                        Else
                            ' Do something else
                        End If              
                    

You could also use String.Equals to find out if two strings have the same value

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.