Delphi中的空字符串 [英] Empty strings in Delphi

查看:73
本文介绍了Delphi中的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:

WideCompareStr(FName,'')<>0

即使FName设置为"也返回false.

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?

清除问题:我有以下代码来测试是否一个宽字符串变量是空字符串.

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;

即使FName设置为''(我在调试器中检查了它),该函数也返回true.在插入trim(FName)和trim(FInfo)而不是变量之后,它返回了所需的结果.

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.

我错过了一些必不可少的东西吗?我使用的编译器是Borland Delphi 2006

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

推荐答案

如果两个字符串相等,WideCompareStr返回0.所以代码:

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

WideCompareStr(FName,'')<>0

返回false,因为两个字符串都相等,这就是您所期望的(我想是!).

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

我现在很困惑.我刚刚检查了以下代码:

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;

r1和r2均为零,这与预期的一样.第二行实际上是语法错误(Trim只能接收一个参数).

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).

这篇关于Delphi中的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆