Visual Basic String Contains Method

In visual basic, the string Contains method is useful to check whether the specified substring exists in the given string or not and it will return a boolean value.

 

In case, if substring exists in string, the Contains method will return true otherwise it will return false.

 

Following is the pictorial representation of checking whether the substring exists in the given string or not using Contains() method in visual basic programming language.

 

Visual Basic String Contains Method Representation Diagram

 

If you observe the above diagram, we are checking whether “Welcome” text exists in “Welcome to Tutlane” string or not using Contains method and it returns true because “Welcome” substring exists in the given string.

Visual Basic String Contains Method Syntax

Following is the syntax of defining a Contains method to check whether the substring exists in the given string or not in visual basic programming language.

 

Public Function Contains(ByVal value As String) As Boolean

If you observe the above syntax, the Contains method will check whether the substring value exists or not and it will return a boolean value.

Visual Basic String Contains Method Example

Following is the example of using Contains() method to check whether the given value occurs within the string or not in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim msg As String = "Welcome to Tutlane"

        Dim subtxt As String = "Tutlane"

        Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt))

        Dim subtxt1 As String = "tutlane"

        Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, msg.Contains(subtxt1))

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used a Contains() method to determine whether the given substring value is found in the string or not and returning a bool value.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic String Contains Method Example Result

 

If you observe the above result, the Contains() method performing a case sensitive operation that’s the reason for “Tutlane” substring it returns “True” and for “tutlane” substring it returns False.

Visual Basic String Contains Case Insensitive

To perform case-insensitive string comparison, we need to use string IndexOf method. Following is the example of performing a case insensitive search in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim msg As String = "Welcome to Tutlane"

        Dim subtxt As String = "Tutlane"

        Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt))

        Dim subtxt1 As String = "tutlane"

        Dim comp As StringComparison = StringComparison.OrdinalIgnoreCase

        Dim result As Boolean = If(msg.IndexOf(subtxt1, comp) > 0, True, False)

        Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, result)

        Console.WriteLine(vbLf & "Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used a string IndexOf method to perform a case insensitive search. We will learn more about string IndexOf method in the next chapters.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic String Contains Case Insensitive Example Result

 

If you observe the above result, for both substrings “Tutlane” and “tutlane” we got a result as True.

 

This is how we can use Contains() method to check whether the specified substring exists in given string or not in visual basic programming language.