VB.NET IndexOf

IndexOf

IndexOf returns the index of a substring. It scans the String. If the substring is not found, it returns -1. IndexOf is part of the .NET Framework's base class library. It is often useful—but there are some tricks to using it correctly.

Example

First, here we locate the first index of a String in a source String. The example has an input String, and we search for the location of "Vader" with the IndexOf function. If the string is not found, the result is -1.

Program that uses IndexOf: VB.NET

Module Module1
    Sub Main()
	' The input string you are using
	Dim s As String = "Darth Vader has pulmonary problems."

	' Does the string contain "Vader"?
	If (s.IndexOf("Vader") <> -1) Then
	    Console.Write("string contains 'Vader'")
	End If

	' Finished
	Console.ReadLine()
    End Sub
End Module

Output

string contains 'Vader'

In the example, the string "Vader" is found in the input String, at index 6. The console program then prints the message inside the If-statement. The final statement ReadLine is there so you can run the program without it exiting.

Console

Loop

Arrow indicates looping

Here we look at looping with IndexOf on Strings. Sometimes you need to locate the first index of a String, and then continue locating further instances. 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.

Program that uses IndexOf and While: VB.NET

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

	' The interation dim
	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

The Do While loop continues until IndexOf returns -1. If the letter never occurs, the loop will never be entered. The loop prints out the Substring located at the index found by IndexOf. The three lines printed start with the letter "a".

Functions

Squares

Here we look at an overview of these methods in the .NET Framework. There are four Functions in the IndexOf family available in VB.NET. Usually you will 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.

IndexOf, Substring

Programming tip

Here we see a simple example of using the Substring Function in VB.NET with the result of IndexOf. The IndexOf call below locates the first index of the uppercase letter 'B'. You can find more details on the Substring function.

Substring
Program that uses Substring: VB.NET

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

IndexOfAny receives an array of characters as the argument and then returns the index of the first one found. You could call IndexOf many times to duplicate the functionality of IndexOfAny. But the IndexOfAny function is simpler to call.

IndexOfAny

Contains

True keyword

In the .NET platform, the Contains Function on System.String simply calls IndexOf internally. Therefore, you should only use it if you want a different value to indicate failure—False instead of -1.

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

Performance optimization

The most important performance tip I know is to use a character 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 the same.

IndexOf

Summary

The VB.NET programming language

We covered several examples of using the IndexOf Function. We used IndexOf with Strings and characters. Next we called it in Do While-loops. And we checked its return value, which is negative one if the argument is not found.

Review:This is a powerful Function in Visual Basic .NET and can simplify your program. Use it instead of duplicating For-loops.


VB.NET: String