Home
VB.NET
String Contains Example
This page was last reviewed on 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 25, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.