當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


VB.NET InStr()用法及代碼示例

InStr() 函數用於搜索指定字符串中的子字符串。

用法:

InStr(Str, SubStr)

參數:

  • Str:指定的字符串,可能包含給定的子字符串。
  • SubStr:指定要搜索的子串。

返回值:

InStr() 函數將返回一個整數值,如果找到一個子字符串,則它將返回該字符串中子字符串的起始索引。

程序/源代碼:

下麵給出了演示 InStr() 函數的源代碼。給定的程序已成功編譯並執行。

'VB.Net program to demonstrate the InStr() function.

Module Module1

    Sub Main()
        Dim str As String = "I love india"
        Dim substr As String = "love"

        Dim ret As Integer = 0

        ret = InStr(str, substr)

        If ret = 0 Then
            Console.WriteLine("Substring {0} is not found", substr)
        Else
            Console.WriteLine("Substring {0} is found at index {1}", substr, ret)
        End If

    End Sub
    
End Module

輸出:

Substring love is found at index 3
Press any key to continue . . .

說明:

在上麵的程序中,我們創建了一個包含 Main() 方法的模塊 Module1。在Main()方法中,我們創建了三個變量str、substr、ret,分別初始化為“我愛印度”、"love"、0。

ret = InStr(str, substr)

在上麵的代碼中,我們使用了 InStr() 函數,它將返回一個整數值,如果找到一個子字符串,那麽它將返回字符串中子字符串的起始索引。





相關用法


注:本文由純淨天空篩選整理自 VB.Net program to demonstrate the InStr() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。