TheDeveloperBlog.com

Home | Contact Us

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

VB.NET IndexOf Function Examples

This VB.NET tutorial shows the IndexOf Function. IndexOf returns the index of a substring.

IndexOf. This function returns the index of a substring.

First it scans the String. And if the substring is not found, it returns -1.

A helpful method, IndexOf is part of the .NET Framework's base class library. It is often useful. But there are some tricks to using it correctly.

An example. First, here we locate the first index of a String in a source String. We search for the location of "dog" with the IndexOf function.

Not found: If the string is not found, the result is -1. Often this is an important thing to test.

Result: In the example, the string "dog" is found in the input String, at index 5. It is not -1.

Finally: The console program prints the message inside the If-statement. True is written to the console.

Based on:

.NET 4.5

VB.NET program that uses IndexOf

Module Module1
    Sub Main()
	' Our input string.
	Dim animals As String = "cat, dog, bird"

	' See if dog is contained in the string.
	If Not animals.IndexOf("dog") = -1 Then
	    Console.WriteLine(True)
	End If
    End Sub
End Module

Output

True

Loop. Here we look at looping with IndexOf on Strings. Sometimes we need to locate the first index of a String, and then continue locating further instances.

Do while: You can accomplish this with a Do While construct and the IndexOf method.

Here: The example first declares the Dim String, which contains the letter 'a' in three places. Next, we call IndexOf on the String.

Info: The Do While loop continues until IndexOf returns -1. If the letter never occurs, the loop will never be entered.

VB.NET program that uses IndexOf, While

Module Module1
    Sub Main()
	' The input String.
	Dim s As String = "You have a cat"

	' The iteration variable.
	Dim i As Integer = s.IndexOf("a"c)

	' Loop over the found indexes.
	Do While (i <> -1)
	    ' Write the substring.
	    Console.WriteLine(s.Substring(i))
	    ' Get next index.
	    i = s.IndexOf("a"c, i + 1)
	Loop

	Console.ReadLine()
    End Sub
End Module

Output

ave a cat
a cat
at

Functions. There are four functions in the IndexOf family available in VB.NET. Often we only need the first (IndexOf). But the others are occasionally valuable.

IndexOf: This finds the first index of a Char or String. You can specify start and ending indexes of where you want to search.

IndexOfAny: This function finds the first index of any of the characters in the Array you send it.

LastIndexOf: This starts searching from the end. It is the same as IndexOf in all other ways.

LastIndexOf

LastIndexOfAny: This finds the last index of any of the characters in the Array it receives.

LastIndexOfAny

IndexOf, Substring. Here we use the Substring function with the result of IndexOf. The IndexOf call here locates the first index of the uppercase letter B in the source string.

VB.NET program that uses Substring

Module Module1
    Sub Main()
	' The string you are searching.
	Dim s As String = "Visual Basic rocks"

	' Find index of uppercase letter B.
	Dim i As Integer = s.IndexOf("B"c)

	' This new string contains the substring starting at B.
	Dim part As String = s.Substring(i)
	Console.WriteLine(part)
	Console.ReadLine()
    End Sub
End Module

Output

Basic rocks

IndexOfAny. This receives an array of Chars as the argument. It then returns the index of the first one found. This is a way to simplify a search for multiple values.

IndexOfAny

Contains. This System.String Function calls IndexOf internally. We should only use it if we want a different value (False) to indicate failure.

Contains

Contains: Result when substring exists: True. Result when substring does NOT exist: False.

IndexOf: Result when substring exists: >= 0. Result when substring does NOT exist: -1.

Performance. Often we can use a Char instead of a one-character String. In some cases, the character overload is 85% faster. It will return an equivalent value.

Note: A detailed benchmark is available on the C# version of this article. The underlying implementations are shared.

Several examples. We covered several examples of using the IndexOf Function. We used IndexOf with Strings and Chars. Next we called it in a Do-While loop.

Return value. We checked IndexOf's result, which is negative one if the argument is not found. This function can simplify a program. It can replace many For-loops that search strings.


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