在VB中,可以使用InStr
函數來查找一個字符串在另一個字符串中的位置。InStr
函數有多種用法,具體取決于你要實現的功能。
Dim str As String = "Hello, World!"
Dim subStr As String = "World"
Dim position As Integer = InStr(str, subStr)
If position > 0 Then
Console.WriteLine("子字符串的位置是:" & position)
Else
Console.WriteLine("子字符串不存在")
End If
Dim str As String = "Hello, World!"
Dim subStr As String = "o"
Dim startPosition As Integer = 5
Dim position As Integer = InStr(startPosition, str, subStr)
If position > 0 Then
Console.WriteLine("子字符串的位置是:" & position)
Else
Console.WriteLine("子字符串不存在")
End If
Dim str As String = "Hello, World!"
Dim subStr As String = "o"
Dim position As Integer = InStrRev(str, subStr)
If position > 0 Then
Console.WriteLine("子字符串最后一次出現的位置是:" & position)
Else
Console.WriteLine("子字符串不存在")
End If
InStr
函數還有其他用法,你可以根據具體需求來選擇使用。