Delphi – Empty strings in Delphi

delphi

my problem is as follows:

WideCompareStr(FName,'')<>0

returns false even with FName set to ''.

WideCompareStr(trim(FName),'')<>0

returns the desired result. Why do I have to trim an empty string ('') for a comparison with another empty sting to get the correct result?


EDIT:

to clear things up:
I had the following Code to test wether a widestring-variable is the empty string or not.

function TSybVerwandlung.isEmpty: Boolean;
var
  I : Integer;
begin
  Result:=true;
  if WideCompareStr(FName,'')<>0 then Result:=false
  else if WideCompareStr(FInfo,'')<>0 then Result:=false
  else
  begin
    //additional tests
  end;
end;

This function returned true even with FName set to '' (I checked it in the debugger). After inserting trim(FName) and trim(FInfo) instead of the variables, it returned the desired result.

Did I miss something essential? The compiler I use is Borland Delphi 2006

Best Solution

WideCompareStr returns 0 if both strings are equal. So code:

WideCompareStr(FName,'')<>0

Returns false because both strings ARE equal which is what you are expecting (I guess!).

EDIT:

I am confused now. I just checked and in following code:

procedure TForm1.Button1Click(Sender: TObject);
var
  s1: WideString;
  r1, r2: Integer;
begin
  s1 := '';

  r1 := WideCompareStr (s1, '');
  MessageDlg (IntToStr (r1), mtWarning, [mbOK], 0);

  r2 := WideCompareStr (Trim (s1), '');
  MessageDlg (IntToStr (r2), mtWarning, [mbOK], 0);
end;

Both r1 and r2 are zero which is as expected. And your second line is actually a syntax error (Trim can receive only one parameter).

Related Question