|
-
Sep 14th, 2009, 03:09 AM
#1
[RESOLVED] Null strings
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...
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 14th, 2009, 03:24 AM
#2
Re: Null strings
I prefer
vb Code:
If Len(Trim(myStr)) = 0 then
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 14th, 2009, 06:12 AM
#3
Re: Null strings
 Originally Posted by koolsid
I prefer
vb Code:
If Len(Trim(myStr)) = 0 then
Any special reason?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 14th, 2009, 06:15 AM
#4
Re: Null strings
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
-
Sep 14th, 2009, 06:18 AM
#5
Re: Null strings
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 14th, 2009, 06:24 AM
#6
Re: Null strings
 Originally Posted by si_the_geek
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
Thanks, I had tried with vbnullstr and was surprised that it was not acknowledged as a vb constant.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 14th, 2009, 06:31 AM
#7
Re: Null strings
 Originally Posted by koolsid
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 
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 14th, 2009, 06:33 AM
#8
Re: Null strings
Thanks, I had tried with vbnullstr and was surprised that it was not acknowledged as a vb constant.
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).
-
Sep 14th, 2009, 06:52 AM
#9
Re: Null strings
 Originally Posted by si_the_geek
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).
I always forget about ctrl/space. At any rate, I assume vbnull is for objects, but what would the difference be between vbnullchar and vbnullstring?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 14th, 2009, 08:20 AM
#10
Re: [RESOLVED] Null strings
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|