在VB中,你可以使用IndexOf
方法来查找字符串中字符的位置。该方法接受一个字符作为参数,并返回字符在字符串中第一次出现的位置。以下是一个示例:
Dim str As String = "Hello, World!"
Dim ch As Char = "o"
Dim position As Integer = str.IndexOf(ch)
If position >= 0 Then
Console.WriteLine("Character '{0}' found at position {1}", ch, position)
Else
Console.WriteLine("Character '{0}' not found", ch)
End If
输出结果将是:
Character 'o' found at position 4
请注意,IndexOf
方法返回的位置是从零开始的索引,如果字符不存在于字符串中,则返回-1。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:vb怎么查找字符串指定字符