I have read somewhere in the forums that this statement:
If myStr = "" Then
is less convenient (efficient?) than:
If myStr = vbNull Then
But I can't remember if the correct constant above is vbNull, vbNullChar, vbNullStr or what...
Printable View
I have read somewhere in the forums that this statement:
If myStr = "" Then
is less convenient (efficient?) than:
If myStr = vbNull Then
But I can't remember if the correct constant above is vbNull, vbNullChar, vbNullStr or what...
I prefer
vb Code:
If Len(Trim(myStr)) = 0 then
The constant you want is vbNullString
It uses slightly less memory, and is slightly faster. Apparently the fastest way is to use LenB, eg:
Code:If LenB(myStr) = 0 Then
vb Code:
Sub Sample() Dim Strg As String Strg1 = "" Strg2 = " " '~~> If we look at it both strings are empty though the 2nd one has an empty space If Strg1 = vbNullString Then MsgBox "Sting 1 is Empty" If Len(Trim(Strg2)) = 0 Then MsgBox "Sting 2 is Empty" End Sub
Edit: Sigh! Speedy Si at work :D
I see what you mean, but " " may or may not be considered an empty string, depending on the specific situation.
I started the thread because I wanted to prevent errors when reading a text file line-wise. If there was a vbcrlf at the end of the last non-empty line then a null string was read next.
There's no need to remember (or type) the whole thing, because VB will happily tell you what they all are.. just type vbNu and press Ctrl-Space (and if you type vbNulls it will complete it for you).Quote:
Thanks, I had tried with vbnullstr and was surprised that it was not acknowledged as a vb constant.
vbNull is actually a number, for comparing with the result of (I think) the VarType function.
vbNullChar is the same as Chr$(0), but quicker because it is a constant rather than a function.
vbNullString is a string with a null pointer (which is needed for some API's), but within VB it is the equivalent of an empty string.