问题
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
回答1:
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).
回答2:
WideCompareStr NEVER returns FALSE! ( it does not return a boolean value )
instead, it returns 0, if S1=S2.
if S1 <> S2, then:
it either returns a negative value if S1 < S2
or
a positive value if S1 > S2.
Also: In Delphi, Length(S) never calls strlen(). All string types (Shortstring, AnsiString, WideString and UnicodeString * ) contain a length field, which is a 32-bit integer (except in the Shortstring type it is only 8 bits).
So, in All strings except Shortstring a length() call will first check if the string variable is NIL, if it is, a ZERO is immediately returned, otherwise the content of the length field is returned. Absolutely NO memory scanning for a terminating NULL character.
- = the UnicodeString type is available in Delphi 2009 or newer. Delphi 2009 and newer also redefines:
type
String = UnicodeString; // in older Delphis: String = AnsiString;
Char = Widechar; // in older Delphis: Char = AnsiChar;
回答3:
Why don't you simply use Length? Whenever you will have an empty string it will return you zero.
回答4:
I'm not that deep into widestrings, and if you compare purely on widestring level (not using pwidechar typecasts) it shouldn't be the problem, BUT
I know that with ansistrings that there are two states of empty. one is that the pointer value of the string is NIL, the other one is that it is assigned to some "XXXX_EMPTYCHAR" #0#0 constant. (the exact name of the symbol might vary between Delphi and FPC). If some of the routines compare using pointers, weird results may happen. Strangely enough above seems to be all RTL, which usually carefully checks this.
回答5:
I hate to state the obvious, but it must be because you have spaces in your string, perhaps you can build a function that does that for you and you can use for comparision across your application.
My two cents
回答6:
Maybe this test will shed some light on the matter:
procedure TForm1.Button1Click(Sender: TObject);
var
s1: string;
s2: string;
begin
s1 := ' ';
s2 := '';
if WideCompareStr(s1, '') <> 0 then showmessage('s1 test');
if WideCompareStr(s2, '') <> 0 then showmessage('s2 test');
end;
回答7:
In C++ Builder, there is an IsEmpty() function. However, I cannot see how to get that working from Delphi. I've just asked a question on that topic.
来源:https://stackoverflow.com/questions/1017469/empty-strings-in-delphi