VB / VBA StrComp 或 =
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB / VBA StrComp or =
提问by Barry-Jon
What, if anything, is the benefit of using
如果有的话,使用的好处是什么
If StrComp(strVal1, strVal2, vbTextCompare) = 0 Then
as opposed to using
而不是使用
If strVal1 = strVal2 Then
If Option Compare Text
is set at the module level, is there any difference?
如果Option Compare Text
是在模块级别设置,有什么区别吗?
I know StrComp handles null scenarios and <> scenarios, I am only interested in the situation where strVal1 and strVal2 have non-null valid strings assigned.
我知道 StrComp 处理空场景和 <> 场景,我只对 strVal1 和 strVal2 分配了非空有效字符串的情况感兴趣。
采纳答案by Konrad Rudolph
If
Option Compare Text
is set at the module level, is there any difference?
如果
Option Compare Text
是在模块级别设置,有什么区别吗?
No. It simply offers a finer grained control (no module-level strategy commitments). However, if you canmake such a commitment, go for the x = y
option: less code is always better code.
不。它只是提供更细粒度的控制(没有模块级策略承诺)。但是,如果您可以做出这样的承诺,请x = y
选择:更少的代码总是更好的代码。
回答by gbianchi
Since StrComp is comparing string (with cultural info), UpperCase and LowerCase are not taking care ... (so Hello is the same as hello). In the case of =, there will be different (Like using a Binary compare). If option compare text is at module level, there will be no difference (but you should use StrComp in case another guy delete it)...
由于 StrComp 正在比较字符串(带有文化信息),因此 UpperCase 和 LowerCase 不会在意......(所以 Hello 与 hello 相同)。在 = 的情况下,会有不同(就像使用二进制比较)。如果选项比较文本在模块级别,则没有区别(但您应该使用 StrComp 以防其他人删除它)...