If expression; Whether or not to StrComp()?

Which of these if tests are typically preferred and why?
The first one seems to be a little more readable.
What are the pros/cons between them? Performance? Footprint? Etc.?

If str_var <> "" Then

If StrComp(str_var, "") <> 0 Then

The VBA way is even slightly more readable. But VBA support needs are being removed from the project.
If Not str_var = "" Then

Thanks

This shouldn’t depend on VBAsupport!
The relevant difference is that the first attempt only looks for equality (or its absence) while the second one will also tell which argument is considered “GT” (GreaterThan) in case of inequality.

1 Like

All of them works in StarBasic (without VBA compatibility options):

Function MyStringCompare1(Str1 as string, Str2 as string) as boolean
 MyStringCompare1 = (Str1 <> Str2)
end function


Function MyStringCompare2(Str1 as string, Str2 as string) as boolean
 MyStringCompare2 = StrComp(Str1,Str2) <> 0 
end function


Function MyStringCompare3(Str1 as string, Str2 as string) as boolean
 MyStringCompare3 = Not(Str1 = Str2)
end function

Hey all. Thanks for your inputs. Very informative.
However the “NOT” version does not work for me without VBA support. It works without the “NOT” qualifier. But not with.

'Option VBASupport 1

Sub Main()
	If NOT "STRING" = "" Then
		MsgBox("Not Same")
	End If
End Sub

Inadmissible value or data type.
Data type mismatch.

Doesn’t like the “NOT” qualifier without VBA support option.
Guessing that the NOT is being applied to “STRING” so then the comparison is between a Boolean and a string. Placing parentheses around the strings comparison works. Then the NOT is applied to the comparison result.

’ Version: 7.6.6.3 (X86_64) / LibreOffice Community
’ Build ID: d97b2716a9a4a2ce1391dee1765565ea469b0ae7
’ CPU threads: 8; OS: Windows 10.0 Build 22631; UI render: Skia/Raster; VCL: win
’ Locale: en-US (en_US); UI: en-US
’ Calc: threaded

1 Like

if Not (str_var = "") Then works with StarBasic as well.

See this help page.

This is the preferred method to check for inequality. It only does specifically what is requested: checks if the arguments are not equal (using any possible optimizations, e.g. checking first, if the knows string lengths are not equal).

This is definitely not preferred method for the specific task of checking if these are not equal. This method returns the strings’ lexicographical order; for that, it can’t be as efficient as the simple <>: e.g., it can’t shortcut on inequal lengths; and even can’t simply compare each pair of characters, because it needs not only know if they are inequal, but also which is first - and even here, it can’t simply use the characters’ numeric values, but must take collation into account, respecting current locale. It has a complex processing of the case-insensitive mode, and the default case sensitivity depends on compatibility mode. It does a lot of work, and you then only check if the returned value is not 0.

This is less readable than <>, and also a little bit less efficient than that, because it uses an extra Basic call for subsequent Not. But it works irrespective of the VBA support.

2 Likes