Recent

Author Topic: [solved] How do I use IsEmptyStr function  (Read 5503 times)

bzman24

  • Jr. Member
  • **
  • Posts: 71
[solved] How do I use IsEmptyStr function
« on: November 01, 2014, 03:45:49 am »
Hello,

Can someone please give me a code example of how to use the IsEmptyStr function.  I have tried the following but neither works.  I've searched all over and can't find any working examples of this.

Thanks, Bzman

IF IsEmptyStr(Line) = True THEN ShowMessage('Empty Line!');

IF IsEmptyStr(Line, Chr(09)) = True THEN ShowMessage('Empty Line!');
« Last Edit: November 02, 2014, 02:41:03 am by bzman24 »
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: How do I use IsEmptyStr function
« Reply #1 on: November 01, 2014, 04:35:52 am »
http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/isemptystr.html

e.g.
procedure TForm1.FormClick(Sender: TObject);
var
  emptyStr : String = '';{or something else(e.g. 'abcd')}
  j : integer;
  validSysChar : TSysCharSet;
begin
  for j := 1 to Length(emptyStr) do
    Include(validSysChar, emptyStr[j]);
  if(IsEmptyStr(Edit1.Text, validSysChar)) then    //{Edit1.Text - check this string}
    ShowMessage('It is')
  else
    ShowMessage('It is not');
end;       

p.s. http://stackoverflow.com/questions/561457/how-to-pass-a-string-variable-as-a-tsyscharset 
« Last Edit: November 01, 2014, 05:05:41 am by exdatis »

sapper

  • New Member
  • *
  • Posts: 35
Re: How do I use IsEmptyStr function
« Reply #2 on: November 01, 2014, 12:19:12 pm »
Code: [Select]
  if IsEmptyStr(line,[Chr(09)]) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 


Code: [Select]
  if IsEmptyStr(line,[#9]) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 

Code: [Select]
  if IsEmptyStr(line,[0..' ']) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: How do I use IsEmptyStr function
« Reply #3 on: November 01, 2014, 04:54:37 pm »
Code: [Select]
  if IsEmptyStr(line,[Chr(09)]) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 
   exdatis: //works ok


Code: [Select]
  if IsEmptyStr(line,[#9]) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 
   exdatis: //works ok

Code: [Select]
  if IsEmptyStr(line,[0..' ']) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 
exdatis:  //I've got an error: unit1.pas(46,32) Error: Incompatible types: got "Constant String" expected "Byte"


So, much more simple than my example! ;)
Regards
« Last Edit: November 01, 2014, 04:56:53 pm by exdatis »

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I use IsEmptyStr function
« Reply #4 on: November 02, 2014, 12:37:52 am »
Thanks guys!  I Knew it had to be something like that but the braces are the key.  Thanks.  Never thought of using them.

Maybe you can explain something else to me about this.  Here is how I am using this.  I have a text file that has several lines in it that are interwoven with a tabbed line and spaced lines as such:

data line
line with just tab space(chr(09))
data line
line with just tab space(chr(09))
data line
line with just tab space (chr(09))
data line
line with 2 spaces (chr(20)chr(20))
data line
line with just tab space(chr(09))
data line
line with 1 space (chr(20))
data line
line with just tab space(chr(09))
data line

no order to where the tabs or spaces are - just alternating between the data lines.

By using the following code (your example), it works but it also leaves the lines with spaces.  I am trying to filter out the tab and space lines.

BEGIN
ReadLn(InputFile, Line1);
IF IsEmptyStr(Line1,[Chr(09)]) THEN Continue
ELSE Writeln(OutputFile, Line1);
END;

I tried IsEmptyStr(Line1,[Chr(09),Chr(20)]) and it runs without any errors but does not filter out the space lines.  I tried IsEmptyStr(Line1,[Chr(09)..Chr(20)]) also but it didn't work either.  Various other combinations of &, +, ... were tried but they all generated compile errors.  Both examples filter out the tab lines but not the space lines.  Can I filter out both types of blank lines with this function?

Thanks again, Bzman
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

sapper

  • New Member
  • *
  • Posts: 35
Re: How do I use IsEmptyStr function
« Reply #5 on: November 02, 2014, 02:13:10 am »
Isn't space #32?

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I use IsEmptyStr function
« Reply #6 on: November 02, 2014, 02:22:50 am »
How right you are!  20 is the hex of decimal 32.   :-[ :-[

Let me try that.  Maybe it will work if I use the correct number.

Thanks,  Bzman24
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

sapper

  • New Member
  • *
  • Posts: 35
Re: How do I use IsEmptyStr function
« Reply #7 on: November 02, 2014, 02:25:37 am »
You could also do this instead.

Code: [Select]
line := SysUtils.Trim(line);
if line = '' then WriteLn('Empty');


Quote
exdatis:  //I've got an error: unit1.pas(46,32) Error: Incompatible types: got "Constant String" expected "Byte"


Code: [Select]
  if IsEmptyStr(line,[0..' ']) then
     WriteLn('Empty')
  else
    Writeln('Not Empty'); 

Works in CodeTyphon 5.1 on Windows 8.1 using fpc 2.7.1.

bzman24

  • Jr. Member
  • **
  • Posts: 71
[solved] Re: How do I use IsEmptyStr function
« Reply #8 on: November 02, 2014, 02:40:09 am »
Yep.  That was it.  When I fixed the error the following code works exactly as is should.

   BEGIN
      ReadLn(InputFile, Line1);
      IF IsEmptyStr(Line1,[Chr(09),Chr(32)]) THEN Continue
      ELSE Writeln(OutputFile, Line1);
   END;

Yes, your example of using Trim does work and that is what I was using to get rid of the space lines but I was hoping to do the filtering in a more concise way and that is when I found IsEmptyStr in the RTL.  Thanks Sapper for helping me out on this and boy am I embarrassed by the hex / decimal numbering boo boo.

Thanks exdatis also.

Once again this forum solves another one.

Bzman24 :)
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

 

TinyPortal © 2005-2018