InStrRev Function in VB.NET

What is the use of InStrRev function in VB.NET?

Explanation

InStrRev Function

InStrRev String Function in Visual Basic.net 2008 is used return the index of the first occurence of a substring inside a string by searching from right to left.
Syntax:

Function InStrRev(ByVal StringCheck As String,
ByVal StringMatch As String, Optional ByVal Start As
Integer = -1,Optional ByVal Compare As
CompareMethod ) As Integer

In the above syntax StringMatch specifies the string to be searched for its first occurence inside the string StringCheck starting from right to left. Start is an optional value to specify the starting position for the search, by default it is -1, so the search is done from right to left, if a value is specified the search is done from left to right. A compare method to specify the type of comparision
Example:

Module Module1
Sub Main()
Sub Main()
Console.WriteLine("Index of First Occurence of 'a' is::"
& InStrRev("a is a alphabet", "a"))
Console.WriteLine("Index of First Occurence of 'b' is::"
& InStrRev("b is b alphabet", "b", 5))
Console.ReadLine()
End Sub
End Sub
End Module
Result:

Index of First Occurence of 'a' is::12
Index of First Occurence of 'b' is::1

In the above example, the InStrRev function returns 12 as the index a since the starting value for the start value by default is set to the end of string. But for b the start value is set to '5' so the search is done from left to right so the index is '1'.

Visual Basic Tutorial


Ask Questions

Ask Question