A common condition that all programs should do is to check if string are empty or not.
Take the below statements:
(1)
if Length(Str)=0 then // do something
(2)
if Str='' then // do something
Java String isEmpty() Method The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.
Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty.
Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.
In XE2, if str = ''
compiles to a better faster code for Ansi and Unicode strings. if Length(str) = 0
compiles to a better faster code for Wide strings.
Test program:
{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N-,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1} program Project172; {$APPTYPE CONSOLE} {$R *.res} var sa1,sa2: AnsiString; sw1,sw2: WideString; su1,su2: UnicodeString; begin if Length(sa1) = 0 then ; if sa2 = '' then ; if Length(sw1) = 0 then ; if sw2 = '' then ; if Length(su1) = 0 then ; if su2 = '' then ; end.
Compiled code:
Project172.dpr.14: if Length(sa1) = 0 then 004050E2 A19C9B4000 mov eax,[$00409b9c] 004050E7 85C0 test eax,eax 004050E9 7405 jz $004050f0 004050EB 83E804 sub eax,$04 004050EE 8B00 mov eax,[eax] 004050F0 85C0 test eax,eax Project172.dpr.16: if sa2 = '' then 004050F2 833DA09B400000 cmp dword ptr [$00409ba0],$00 Project172.dpr.18: if Length(sw1) = 0 then 004050F9 A1A49B4000 mov eax,[$00409ba4] 004050FE 85C0 test eax,eax 00405100 7407 jz $00405109 00405102 83E804 sub eax,$04 00405105 8B00 mov eax,[eax] 00405107 D1E8 shr eax,1 00405109 85C0 test eax,eax Project172.dpr.20: if sw2 = '' then 0040510B A1A89B4000 mov eax,[$00409ba8] 00405110 33D2 xor edx,edx 00405112 E839E8FFFF call @WStrEqual Project172.dpr.22: if Length(su1) = 0 then 00405117 A1AC9B4000 mov eax,[$00409bac] 0040511C 85C0 test eax,eax 0040511E 7405 jz $00405125 00405120 83E804 sub eax,$04 00405123 8B00 mov eax,[eax] 00405125 85C0 test eax,eax Project172.dpr.24: if su2 = '' then 00405127 833DB09B400000 cmp dword ptr [$00409bb0],$00
The difference is even bigger if optimization is disabled.
Project172.dpr.14: if Length(sa1) = 0 then 004050E2 A19C9B4000 mov eax,[$00409b9c] 004050E7 8945EC mov [ebp-$14],eax 004050EA 837DEC00 cmp dword ptr [ebp-$14],$00 004050EE 740B jz $004050fb 004050F0 8B45EC mov eax,[ebp-$14] 004050F3 83E804 sub eax,$04 004050F6 8B00 mov eax,[eax] 004050F8 8945EC mov [ebp-$14],eax 004050FB 837DEC00 cmp dword ptr [ebp-$14],$00 Project172.dpr.16: if sa2 = '' then 004050FF 833DA09B400000 cmp dword ptr [$00409ba0],$00 Project172.dpr.18: if Length(sw1) = 0 then 00405106 A1A49B4000 mov eax,[$00409ba4] 0040510B 8945E8 mov [ebp-$18],eax 0040510E 837DE800 cmp dword ptr [ebp-$18],$00 00405112 740D jz $00405121 00405114 8B45E8 mov eax,[ebp-$18] 00405117 83E804 sub eax,$04 0040511A 8B00 mov eax,[eax] 0040511C D1E8 shr eax,1 0040511E 8945E8 mov [ebp-$18],eax 00405121 837DE800 cmp dword ptr [ebp-$18],$00 Project172.dpr.20: if sw2 = '' then 00405125 A1A89B4000 mov eax,[$00409ba8] 0040512A 33D2 xor edx,edx 0040512C E81FE8FFFF call @WStrEqual Project172.dpr.22: if Length(su1) = 0 then 00405131 A1AC9B4000 mov eax,[$00409bac] 00405136 8945E4 mov [ebp-$1c],eax 00405139 837DE400 cmp dword ptr [ebp-$1c],$00 0040513D 740B jz $0040514a 0040513F 8B45E4 mov eax,[ebp-$1c] 00405142 83E804 sub eax,$04 00405145 8B00 mov eax,[eax] 00405147 8945E4 mov [ebp-$1c],eax 0040514A 837DE400 cmp dword ptr [ebp-$1c],$00 Project172.dpr.24: if su2 = '' then 0040514E 833DB09B400000 cmp dword ptr [$00409bb0],$00
Semantically they are identical and there will be no discernible performance difference. So we are left looking at clarity for the reader of the code.
if Str='' then
is the readable version in my opinion. Look at the title of your question:
What is the better way to check for an empty string?
In your head you view this as the empty string as opposed to the string with length 0. So write the code the way that matches your viewpoint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With