VB.Net IndexOf String Function

Updated on 30 Aug 2012,
Published on 01 Jun 2010

In ASP.Net, VB.Net provides IndexOf string function that is most often used to get the position of string pattern passed to IndexOf function. VB.Net IndexOf function returns the index of the first occurrence of the specified string pattern in the provided string. By default IndexOf function starts searching from the 0th index of the string. In VB.Net, IndexOf string function has some overloads that provide the additional functionality to get the index of the specified character or string pattern from the specified starting index and within the specified count or number of characters. Applying some logic to the VB.Net IndexOf string function you can also get the index of specified character or string pattern that occurs after first or second time in the provided string.

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

Following are the commonly used overloads of VB.Net IndexOf string function: 

' 1. 
IndexOf (value As Char) As Integer

' 2. 
IndexOf (value As String) As Integer

' 3. 
IndexOf (value As Char, startIndex As Integer) As Integer

' 4. 
IndexOf (value As String, startIndex As Integer) As Integer

' 5. 
IndexOf (value As String, comarisonType As StringComparison) As Integer

' 6. 
IndexOf (value As Char, startIndex As Integer, count As Integer) As Integer

' 7. 
IndexOf (value As String, startIndex As Integer, count As Integer) As Integer

' 8. 
IndexOf (value As String, startIndex As Integer, comparisonType As StringComparison) As Integer

' 9. 
IndexOf (value String, startIndex As Integer, count As Integer, StringComparison) As Integer

Above mentioned overloads of IndexOf function accept four types of parameters. First parameter refers to char type or string type pattern you want to get the index of. Second type of parameter is integer type that accepts value for start index of the string from which IndexOf string function will start its pattern matching in the associated string value. Third type of parameter is integer type value for count or number of character to search within this specified length of string starting from the specified start index value.

Last parameter type is enumerator of StringComparison. It accepts the following options for comparisonType based on Culture and Case of string letters.

  • CurrentCulture = 0,
  • CurrentCultureIgnoreCase = 1,
  • InvariantCulture = 2,
  • InvariantCultureIgnoreCase = 3,
  • Ordinal = 4,
  • OrdinalIgnoreCase = 5

In ASP.Net, VB.Net IndexOf string function returns the index of specified string pattern if it exists in the string. It returns -1 if specified string pattern does not exist and returns 0 if string is Empty.

Examples of VB.Net IndexOf String Function in ASP.Net

Dim strText As String = "This is a sample text string."

Response.Write("Index of 'a' char: <b>" _
                & strText.IndexOf("a") _
                & "</b>")

Response.Write("<br /><br />")

Response.Write("Index of 'str' string with startIndex=15: <b>" _
                & strText.IndexOf("str", 10) _
                & "</b>")

Response.Write("<br /><br />")

Response.Write("Index of 'str' string with startIndex=4 and count=15: <b>" _
                & strText.IndexOf("str", 4, 15) _
                & "</b>")

Response.Write("<br /><br />")

Response.Write("Index of 'STR' upper case string with startIndex=4, count=25 and CurrentCultureIgnoreCase: <b>" _
                & strText.IndexOf("STR", 4, 25, StringComparison.CurrentCultureIgnoreCase) _
                & "</b>")
Output

Index of 'a' char: 8

Index of 'str' string with startIndex=15: 22

Index of 'str' string with startIndex=4 and count=15: -1

Index of 'STR' upper case string with startIndex=4, count=25 and CurrentCultureIgnoreCase: 22

Above examples show the use of VB.Net IndexOf string function using different overloads of IndexOf function. In the last example with StringComparison comparisonType parameter value CurrentCultureIgnoreCase is used to search within the string without case-sensitive approach.

Output:

Get the output for above discussed code from the following links:

VB.Net IndexOf Function 01

VB.Net IndexOf Function 02

Continue to next tutorial: VB.Net LastIndexOf String function to learn how to get the last occurence of the specified character or string pattern from a string.

0 Responses to "VB.Net IndexOf String Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email