Contains
This function determines if a substring exists in a source string
. It returns a Boolean
that indicates whether the argument string
was located in the instance string
.
You can either directly test the result of the Contains
method, or store it in a local variable (or field). By storing it, you can avoid repeated work and speed up a method.
In this example, we see that there are 2 subroutines defined. The Test subroutine receives one input string
. It prints this to the screen and then invokes the Contains
method on it.
Contains
method's correctness.Module Module1 Sub Main() ' Call the Test sub with different string literal arguments. Test("Dot Net Perls") Test("dot net perls") End Sub Sub Test(ByVal input As String) ' Write the formal parameter. Console.Write("--- ") Console.Write(input) Console.WriteLine(" ---") ' See if it contains this string. Dim net As Boolean = input.Contains("Net") Console.Write("Contains 'Net': ") Console.WriteLine(net) ' See if it contains this string. If input.Contains("perls") Then Console.WriteLine("Contains 'perls'") End If ' See if it does not contain this string. If Not input.Contains("Dot") Then Console.WriteLine("Doesn't Contain 'Dot'") End If End Sub End Module--- Dot Net Perls --- Contains 'Net': True --- dot net perls --- Contains 'Net': False Contains 'perls' Doesn't Contain 'Dot'
We described the Contains
Function and demonstrated its usage. We also showed how its Boolean
result can be stored in memory for later reuse. IndexOf
can also search strings.