C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
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
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
VB.NET program that shows IndexOf results
Module Module1
Sub Main()
Dim source As String = "orange cat"
' See IndexOf function results.
Console.WriteLine("NOT FOUND: {0}", source.IndexOf("dog"))
Console.WriteLine("FOUND: {0}", source.IndexOf("cat"))
End Sub
End Module
Output
NOT FOUND: -1
FOUND: 7
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
Version 1: In this version of the code, we pass a Char to IndexOf and test the result value against-1.
Version 2: This code uses a single-character string as the argument. The end result is the same as version 1, but the performance is worse.
Result: It is much faster to search for a Char, not a 1-letter String. This optimization can help many programs.
BenchmarksVB.NET program that benchmarks IndexOf, Char and String
Module Module1
Sub Main()
Dim letters As String = "abcdef"
Dim m As Integer = 10000000
' Version 1: use char argument with IndexOf.
Dim s1 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
If letters.IndexOf("f"c) = -1 Then
Return
End If
Next
s1.Stop()
' Version 2: use string argument with IndexOf.
Dim s2 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
If letters.IndexOf("f") = -1 Then
Return
End If
Next
s2.Stop()
Dim u As Integer = 1000000
Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
End Sub
End Module
Output
4.98 ns IndexOf(Char)
126.90 ns IndexOf(String)
If: The result value from IndexOfAny should be checked for -1 each time it is called. Use an If-statement.
If ThenTip: Always check for -1, because many times using -1 elsewhere in your application will cause an error. It represents a not-found result.
Warning: The -1 value returned by IndexOf and methods such as IndexOfAny has caused countless bugs.
VB.NET program that uses IndexOfAny
Module Module1
Sub Main()
' The input String.
Dim s1 As String = "The cat is hungry."
' Find the first index of either "x" or "n"
Dim i1 As Integer = s1.IndexOfAny(New Char() {"x"c, "n"c})
If (i1 <> -1) Then
Console.WriteLine(s1.Substring(i1))
End If
' Another input String.
Dim s2 As String = "C# is challenging."
' Find the first index of either '#' or ' '
Dim i2 As Integer = s2.IndexOfAny(New Char() {"#"c, " "c})
If (i2 <> -1) Then
Console.WriteLine(s2.Substring(i2))
End If
Console.ReadLine()
End Sub
End Module
Output
ngry.
# is challenging.
Info: IndexOfAny can be seen as an optimized series of calls to IndexOf. We could duplicate its results by repeatedly calling IndexOf.
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.
LastIndexOfLastIndexOfAny: This finds the last index of any of the characters in the Array it receives.
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.