vb怎么查找字符串指定字符
在VB中,可以使用InStr()
函数来查找字符串中的指定字符。下面是一个示例代码:
Dim str As StringDim charToFind As StringDim position As Integerstr = "Hello, world!"charToFind = "o"position = InStr(str, charToFind)If position > 0 ThenMsgBox "The character " & charToFind & " is found at position " & positionElseMsgBox "The character " & charToFind & " is not found in the string"End If
在此示例中,InStr()
函数会查找字符串str
中第一次出现字符charToFind
的位置,并将结果赋值给变量position
。如果找到了该字符,则会显示一个消息框指示字符的位置;否则显示一个消息框表示字符未找到。