vb.net IndexOf(String, Int32, StringComparison)在字符串中从指定的位置开始搜索指定字符串的第一个匹配项,使用指定的比较规则。详细实例
时间: 2024-02-06 14:11:41 浏览: 31
好的,下面是一个使用`IndexOf(String, Int32, StringComparison)`方法的示例代码:
```vb.net
Dim str As String = "Hello World! This is a sample string."
Dim searchStr As String = "world"
Dim index As Integer = str.IndexOf(searchStr, 0, StringComparison.OrdinalIgnoreCase)
If index <> -1 Then
Console.WriteLine("'{0}' found at index {1}.", searchStr, index)
Else
Console.WriteLine("'{0}' not found in the string.", searchStr)
End If
```
在这个示例中,我们首先定义了一个字符串`str`和一个要搜索的字符串`searchStr`。然后我们使用`IndexOf`方法来查找`searchStr`在`str`中的位置。`0`参数表示从字符串的第一个字符开始搜索,`StringComparison.OrdinalIgnoreCase`参数表示不区分大小写进行比较。
如果`searchStr`在`str`中找到了匹配项,则`IndexOf`方法返回匹配项的起始索引。如果没有找到匹配项,则返回-1。在这个示例中,我们用条件语句检查是否找到了匹配项,并相应地输出结果。
希望这个示例能对你有所帮助!