2013-09-30 82 views
1

我需要比較If語句中的2個變量,但我仍然希望它返回真如果一切都是相同的情況除外。我知道不能寫這個,但這裏是我正在尋找的意義:比較兩個字符串變量忽略大小寫

If (str1=str2 matchcase:=false) then 

任何想法? 感謝

回答

2

如果您正在尋找最快的,你要使用STRCOMP()

STRCOMP會做一些測試,以避免實際的比較,使得它更快的字符串時,將字符串或整個字符串不同。如果你確實需要很多共同的價值觀,你可以避免使用StrComp,但大部分情況下它會更快。

100M comparisons with same phrase but different case: 
LCase = 20 seconds 
StrComp = 23 seconds *This is a rare case 

100M comparisons with different phrase but same length 
LCase = 20 seconds 
StrComp = 9 seconds 

100M comparisons with different phrase and different length 
LCase = 25 seconds 
StrComp = 9 seconds 

注意:字符串的長度會改變結果。例如,在最後一次測試中,LCase花費的時間比其他測試更長,因爲我將其中一個字符串的長度加倍。這減慢了LCase,但不是StrComp。

注2:LCase和UCase有相似的結果。

+0

感謝您的好評! – user2385809

7

If (lcase(str1)=lcase(str2)) then

3

雖然我喜歡@ mr.Reband的答案,但你仍然可以參考這種替代它使用StrComp功能。

Sub test() 

Dim str1 As String 
Dim str2 As String 

str1 = "test" 
str2 = "Test" 

MsgBox StrComp(str1, str2, vbTextCompare) 


'Return Values 
' 
'The StrComp function has the following return values: 
' 
'If StrComp returns 
'string1 is less than string2 -1 
'string1 is equal to string2 0 
'string1 is greater than string2 1 
'string1 or string2 is Null Null 

End Sub 
相關問題