Home
VB.NET
String Contains Example
Updated Sep 25, 2022
Dot Net Perls
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.
Function notes. 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.
For
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.
Then The method will determine if the case-sensitive strings "Net", "perls", or "Dot" are located in the string.
And Because it is called twice, we can test the 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'
Summary. 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.
String IndexOf
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 25, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen