7

Embaracdero documents "IsEmpty" methods for string types, which I've used successfully with C++ Builder code.

WideString s;

if (s.IsEmpty())
   ....

I tried the same from Delphi, and couldn't get it to compile:

var s: WideString;
begin
  if s.IsEmpty then
  ....

I know you can compare with an empty string, or call the Length function, but is it possible to call this IsEmpty method from Delphi?

EDIT: Just to clarify, this wasn't meant as a String vs Widestring issue.

Basically, the docs I link to above describe a Pascal syntax, as well as a C++ one, yet this doesn't seem to work. I assume this is just a flaw in the documentation.

Returns true if the System::WideString::WideString is empty.

Pascal: function IsEmpty: bool;

5
  • What does the C++ implementation look like? Commented Jun 19, 2009 at 12:41
  • 1
    You may not think it's a WideString vs. string issue, but it is. Commented Jun 19, 2009 at 15:11
  • @Craig - The question shouldn't involve strings, only WideStrings. Can you elaborate? Commented Jun 19, 2009 at 15:13
  • 1
    It involved strings before you edited the question to remove them. Commented Jun 19, 2009 at 15:24
  • @Craig - Yup, can't argue with that.... Commented Jun 19, 2009 at 15:46

5 Answers 5

15

String is not a class in Delphi therefore it has no methods, you have to use functions for string manipulations like Length, Copy, etc... String is a class in C++ so maybe you are confused by that.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That's the heart of it: String types are all classes in C++, but not in Delphi. The Codegear documentation is wrong to list pascal equivalents to these methods.
MY guess is that the Pascal equivalents in the help probably refer to Delphi.NET where Strings are classes... (haven't checked this myself yet)
Yes, Delphi documentation is all messed up in these versions but at least CodeGear is trying to gather something better via updates.
5

Delphi is an hybrid language. It contains basic types and classes. Only classes (and records and objects) can contain methods.

String is a basic type, although a special one. It's the only type that has a reserved word. That's why its often written with a lowercase (string) unlike other types which have a starting captial (Integer).

You can if you like:

type
  TString = class
  private
    FString: string;
  public
    constructor Create(const AValue: string);

    property &String: string read FString write FString;
    property IsEmpty: Boolean read GetIsEmpty;
    // ...
  end;

1 Comment

You can prefix a reserved word (like string or while) with a & to use it as a normal identifier.
5

No. string is not WideString, even in D2009. You wouldn't want to, either; comparing with nil/empty string is much faster than a method call.

In Delphi:

var 
  s: string;
begin
  if s = '' then begin
    ShowMessage('It is empty or nil.');

...for string detects both nil and empty string (which is = nil).

4 Comments

Okaayyyy. I get this: [DCC Error] Unit69.pas(31): E2010 Incompatible types: 'string' and 'Pointer' What am I doing wrong? (this with D2007, by the way.)
Error in my example. Fixed. The point is that Delphi actually compares with nil under the hood (look at disassembly).
You can compare (and NEED to compare) strings with nil in Delphi.Net. In native Delphi, it's enough to compare with EmptyStr or '' .
In .NET, use string.IsNullOrEmpty()
2

In the new editions you can use many string helper functions and s.IsEmpty too.

1 Comment

Should be the top answer these days? In System.SysUtils (D10.4) in regards to EmptyStr it says: "{ Empty string and null string pointer. These constants are provided for backwards compatibility only. }" Plus "foo.IsEmpty" compared to "(foo = EmptyStr)" seems much nicer in my opinion.
1
if Trim(s)='' then

???

2 Comments

I know there are other ways. I want to know if IsEmpty can actually be used.
function IsEmpty(s:string):boolean; begin; result:=length(s)=0; end; Now you can use it ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.