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 a string, then 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 the Contains() method in a visual basic programming language.
If you observe the above diagram, we are checking whether the “Welcome” text exists in the “Welcome to Tutlane” string or not using the Contains method and it returns true because “Welcome” substring exists in the given string.
Following is the syntax of defining a Contains method to check whether the substring exists in the given string or not in a 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.
Following is the example of using Contains() method to check whether the given value occurs within the string or not in a 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 returns a bool value.
When we execute the above visual basic program, we will get the result as shown below.
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.
To perform a case-insensitive string comparison, we need to use the string IndexOf method. Following is the example of performing a case insensitive search in a 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 the string IndexOf method in the next chapters.
When we execute the above visual basic program, we will get the result as shown below.
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 a given string or not in a visual basic programming language.