gpt4 book ai didi

vb.net - 字符串中区分大小写/不区分大小写的搜索文本

转载 作者:行者123 更新时间:2023-12-01 08:16:43 26 4
gpt4 key购买 nike

我一直在使用 text.indexof() 来查看一个字符串是否位于另一个字符串内,但是是否可以执行区分大小写/不区分大小写的搜索选项?我一直在谷歌周围寻找,但运气不佳。

如果它可以计算字符串中出现的次数,那将是一个巨大的收获!

最佳答案

IndexOf 有几个重载需要 StringComparison参数,允许您指定各种区域性和区分大小写选项。

例如:

Dim idx As Integer = haystack.IndexOf(needle, StringComparison.OrdinalIgnoreCase)

至于计算出现次数,没有内置的东西,但自己做起来非常简单:

Dim haystack As String = "The quick brown fox jumps over the lazy dog"
Dim needle As String = "th"
Dim comparison As StringComparison = StringComparison.OrdinalIgnoreCase

Dim count As Integer = CountOccurrences(haystack, needle, comparison)

' ...

Function CountOccurrences(ByVal haystack As String, ByVal needle As String, _
ByVal comparison As StringComparison) As Integer
Dim count As Integer = 0
Dim index As Integer = haystack.IndexOf(needle, comparison)
While index >= 0
count += 1
index = haystack.IndexOf(needle, index + needle.Length, comparison)
End While

Return count
End Function

关于vb.net - 字符串中区分大小写/不区分大小写的搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com