英文:
vb.net string.Contains not working as expected
问题
最后一个currentRow.Contains("ctr", strComp)
调用没有找到该字符串在字符串数组中,是因为StringComparer.CurrentCultureIgnoreCase
比较器区分大小写,而搜索的字符串是"ctr",而数组中的字符串是"CTR (%)",它们在大小写上不匹配。因此,最后的Contains调用返回False。如果您希望不区分大小写进行比较,可以更改比较器为StringComparer.OrdinalIgnoreCase
,如下所示:
Dim strComp As StringComparer = StringComparer.OrdinalIgnoreCase
Debug.WriteLine(currentRow.Contains("ctr", strComp)) 'True
这将导致最后的Contains调用返回True,因为它会执行不区分大小写的比较。
英文:
Dim currentRow As String() = {
"Search term",
"Clicks",
"Impressions",
"CTR (%)",
"Avg. CPC",
"Cost",
"All conv. rate",
"Cost / all conv.",
"All conv."
}
Dim strComp As StringComparer = StringComparer.CurrentCultureIgnoreCase
Debug.WriteLine(currentRow.Contains("search term", strComp)) 'True
Debug.WriteLine(currentRow.Contains("clicks", strComp)) 'True
Debug.WriteLine(currentRow.Contains("impressions", strComp)) 'True
Debug.WriteLine(currentRow.Contains("CTR (%)", strComp)) 'True
Debug.WriteLine(currentRow.Contains("ctr (%)", strComp)) 'True
Debug.WriteLine(currentRow.Contains("ctr", strComp)) 'False
Why is it that the last Contains call currentRow.Contains("ctr", strComp)
does not find that string in the string array?
答案1
得分: 0
根据我收到的回复,看起来这是完成此任务的方法:
Dim currentRow As String() = {
"搜索词",
"点击次数",
"展示次数",
"点击率 (%)",
"平均每次点击成本",
"成本",
"所有转化率",
"成本 / 所有转化",
"所有转化次数"
}
Dim strSearch As String = "ctr"
Dim bDidWeFindIt As Boolean = currentRow.Any(Function(str) str.IndexOf(strSearch, StringComparison.OrdinalIgnoreCase) >= 0)
Dim element As String = currentRow.FirstOrDefault(Function(str) str.IndexOf(strSearch, StringComparison.OrdinalIgnoreCase) >= 0)
If element IsNot Nothing Then
' 找到包含 "ctr" 的元素
Console.WriteLine(bDidWeFindIt & " 找到包含的元素: " & element)
Else
' 未找到元素
Console.WriteLine(bDidWeFindIt & " 未找到包含的元素: " & strSearch)
End If
--- Idle_Mind 修改 ---
将 "bDidWeFindIt" 改为 Boolean
类型,并改变语句的顺序。请注意,现在的 If
语句只是检查 "bDidWeFindIt" 的值:
Dim strSearch As String = "ctr"
Dim element As String = currentRow.FirstOrDefault(Function(str) str.IndexOf(strSearch, StringComparison.OrdinalIgnoreCase) >= 0)
Dim bDidWeFindIt As Boolean = element IsNot Nothing
If bDidWeFindIt Then
' 找到包含 "ctr" 的元素
Console.WriteLine(bDidWeFindIt & " 找到包含的元素: " & element)
Else
' 未找到元素
Console.WriteLine(bDidWeFindIt & " 未找到包含的元素: " & strSearch)
End If
英文:
Based on the replies that I received, it appears that this is the way to accomplish this:
Dim currentRow As String() = {
"Search term",
"Clicks",
"Impressions",
"CTR (%)",
"Avg. CPC",
"Cost",
"All conv. rate",
"Cost / all conv.",
"All conv."
}
Dim strSearch as string = "ctr"
Dim bDidWeFindIt As Boolean = currentRow.Any(Function(str) str.IndexOf(strSearch, StringComparison.OrdinalIgnoreCase) >= 0)
Dim element As String = currentRow.FirstOrDefault(Function(str) str.IndexOf(strSearch, StringComparison.OrdinalIgnoreCase) >= 0)
If element IsNot Nothing Then
' Found an element that contains "ctr"
Console.WriteLine(bDidWeFindIt & " Found element that contains: " & element)
Else
' No element found
Console.WriteLine(bDidWeFindIt & " Did not find element that contains: " & strSearch)
End If
--- Edit by Idle_Mind ---
Make "bDidWeFindIt" a Boolean
, and reverse the order of your statements. Note the If
statement now is just checking the value of "bDidWeFindIt":
Dim strSearch As String = "ctr"
Dim element As String = currentRow.FirstOrDefault(Function(str) str.IndexOf(strSearch, StringComparison.OrdinalIgnoreCase) >= 0)
Dim bDidWeFindIt As Boolean = element IsNot Nothing
If bDidWeFindIt Then
' Found an element that contains "ctr"
Console.WriteLine(bDidWeFindIt & " Found element that contains: " & element)
Else
' No element found
Console.WriteLine(bDidWeFindIt & " Did not find element that contains: " & strSearch)
End If
答案2
得分: -1
你也可以使用StartsWith:-
Debug.WriteLine(currentRow.FirstOrDefault(Function(s) s.StartsWith("ctr", StringComparison.CurrentCultureIgnoreCase)) IsNot Nothing)
上述代码将返回True。
英文:
You could use StartsWith as well:-
Debug.WriteLine(currentRow.FirstOrDefault(Function(s) s.StartsWith("ctr", StringComparison.CurrentCultureIgnoreCase)) IsNot Nothing)
The above will return True.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论