String function contains and indexof in VB.NET

In this article we will learn how to use string function contains() and indexof() in VB.NET.
  • 9328
 

In this article we will learn how to use contains and indexof functions in VB.NET.

Contains Function

The Contains method in the VB.NET String Class. Contains method is used to check the specified parameter String occur within the String.

Syntax

Contains (String t2) As Boolean

The contains function returns a Boolean value that means true\false.

Indexof Function

The IndexOf method in String Class returns the index of the first occurrence of the specified substring. The index is 0-based, which means the first character of a string has an index of 0.

Syntax

IndexOf (String str) As Integer

The Indexof method returns the integer value.

For example - The below example defines the contains and indexof Function.

Module Module1

    Sub Main()

' using contains Function.

        Dim t1 As String = " Rohatash Kumar "

        Dim t2 As String = "hat"

        Dim t3 As Boolean

        t3 = t1.Contains(t2)

        Console.WriteLine("Is t1 string has the t2 string: {0}", t3)

' using indexof Function

        Dim str As String

        str = " This is my BOOKS"

        Console.Write("The Index of book is :")

        Console.WriteLine(str.IndexOf("BOOKS"))

    End Sub

End Module

 

OUTPUT

c1.gif 

Figure 1.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.