温馨提示×

vb如何查找字符串中字符的位置

vb
小亿
850
2023-08-06 07:25:26
栏目: 编程语言

在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。

0