TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Contains Example

Use the Contains Function on the String type to see if a substring is found.
Contains determines if a substring exists in a source string. The Contains method provides a clearer name for this. It returns a Boolean that indicates whether the argument string was located in the instance string.StringsBoolean
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, using different search arguments.Sub

Then: The method will determine if the case-sensitive strings "Net", "Codex", or "Dot" are located in the string.

And: Because it is called twice, we can test the Contains method's correctness.

VB.NET program that uses Contains method Module Module1 Sub Main() ' Call the Test sub with different string literal arguments. Test("The Dev Codes") Test("dot net Codex") 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("Codex") Then Console.WriteLine("Contains 'Codex'") 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 Output --- The Dev Codes --- Contains 'Net': True --- dot net Codex --- Contains 'Net': False Contains 'Codex' Doesn't Contain 'Dot'
You can either directly test the result of the Contains method, or store it in a local variable (or field). If you store it, you will not have to execute the internal logic of the method more than once. This can improve tight loops.
Summary. We described the Contains method on the string type. We demonstrated its usage in various program contexts. We also showed how its Boolean result can be stored in memory for later reuse.

Note: For more details on string searching, please check out the IndexOf method.

IndexOf
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf