MajidTaheri
MajidTaheri

Reputation: 3983

What is the better way to check for an empty string in Delphi?

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

Upvotes: 24

Views: 45336

Answers (3)

afrazier
afrazier

Reputation: 4902

To expand on @gabr's answer, if we modify the source to add extra comparisons against EmptyAnsiStr/EmptyWideStr/EmptyStr, we see the following disassemblies from XE1:

Project1.dpr.21: if sa3 = EmptyAnsiStr then
004111DD A1807E4100       mov eax,[$00417e80]
004111E2 8B15EC2C4100     mov edx,[$00412cec]
004111E8 8B12             mov edx,[edx]
004111EA E83148FFFF       call @LStrEqual

Project1.dpr.27: if sw3 = EmptyWideStr then
0041120D A18C7E4100       mov eax,[$00417e8c]
00411212 8B15E82C4100     mov edx,[$00412ce8]
00411218 8B12             mov edx,[edx]
0041121A E8CD49FFFF       call @WStrEqual

Project1.dpr.33: if su3 = EmptyStr then
00411236 A1987E4100       mov eax,[$00417e98]
0041123B 8B15BC2D4100     mov edx,[$00412dbc]
00411241 8B12             mov edx,[edx]
00411243 E8DC4CFFFF       call @UStrEqual

All 3 will require function calls.

Upvotes: 2

gabr
gabr

Reputation: 26830

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

Upvotes: 46

David Heffernan
David Heffernan

Reputation: 612963

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.

Upvotes: 24

Related Questions