溫馨提示×

vb怎么查找字符串指定字符

vb
小億
278
2024-05-17 19:17:11
欄目: 編程語言

在VB中,可以使用InStr()函數(shù)來查找字符串中的指定字符。下面是一個(gè)示例代碼:

Dim str As String
Dim charToFind As String
Dim position As Integer

str = "Hello, world!"
charToFind = "o"

position = InStr(str, charToFind)

If position > 0 Then
    MsgBox "The character " & charToFind & " is found at position " & position
Else
    MsgBox "The character " & charToFind & " is not found in the string"
End If

在此示例中,InStr()函數(shù)會(huì)查找字符串str中第一次出現(xiàn)字符charToFind的位置,并將結(jié)果賦值給變量position。如果找到了該字符,則會(huì)顯示一個(gè)消息框指示字符的位置;否則顯示一個(gè)消息框表示字符未找到。

0