Delphi - check if a string is empty?

Soldato
Joined
8 Jan 2009
Posts
4,819
Location
North East
hi, how can you check if a string is empty, by empty i mean it either contains visible text, if it doesnt or contains spaces and carriage returns then this is still classed as empty.

the below only checks if nothing has been entered, but if the memo contains spaces or carriage returns it thinks the memo contains data:

procedure TForm1.Button1Click(Sender: TObject);
begin
if memo1.lines.text = '' then showmessage('empty') else showmessage('used');
end;

cheers
 
I don't do Delphi, but you could write a function which takes a string input, replaces all special characters (\t, \n, \r) and trims it (to remove extra spaces).

You could then either return this string or return its length in which case a simple if Length = 0 could be used.

edit:
Code:
funtion RemoveNonAlpha(srcStr : string) : string;
var i : integer;
begin
result:='';
for i:=0 to length(srcStr)-1 do
if isCharAlpha(srcstr[i]) then
result:=result+srcStr[i];
end
http://www.delphi3000.com/articles/article_2587.asp?SK=
 
Hi toon_mad,

Try:

if ( Trim(s) <> '' ) then
// its not empty

or:

if ( Trim(s) = '' ) then
// its empty

The trim function gets rid of the special chars.

FredFlint.
 
Back
Top Bottom