How can I delete all empty strings in TRichEdit ?

Can you correct my code:
 Repeat
     Number:=RichEdit1.Lines.Indexof('');
      if Number <> -1 then RichEdit1.Lines.Delete(Number);
 Until Number = -1;

When Delete(Number) has executed 1st time RichEdit1.Lines.Count became equal Number (Why?) and as a result this procedure think that job is over. Where is my mistake?



MasonokAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RBertoraCommented:
here is how:
Rob ;-)

procedure TForm1.Button1Click(Sender: TObject);
var
  number : integer;
begin
  for number := RichEdit1.Lines.Count-1 downto 0 do
  begin
    if RichEdit1.Lines[number] = '' then
      RichEdit1.Lines.Delete(number);
  end;
end;
0
RBertoraCommented:
And there is nothing wrong with your function:
procedure TForm1.Button1Click(Sender: TObject);
var
  number : integer;
begin
  Repeat
     Number:=RichEdit1.Lines.Indexof('');
      if Number <> -1 then RichEdit1.Lines.Delete(Number);
  Until Number = -1;
end;

I just tried it, it works! What is your problem then?

Rob ;-)
0
RBertoraCommented:
Care to leave a comment?
Rob;-)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

MasonokAuthor Commented:
I tried my code by myself it works! but not in my program! :)
Your second choice works very good in my program with only one problem - (RichEdit.Lines.Count-1). After DELETE(Number) RichEdit.Lines.Count became equals Number so if there are 2 emptylines together 1 empty line will be missed!

Could you tell me why  - After DELETE(Number) RichEdit.Lines.Count became equals Number????

0
MasonokAuthor Commented:
Sorry that I didn't write a comments first time :)
0
RBertoraCommented:
>After DELETE(Number) RichEdit.Lines.Count became equals Number so if there are 2 emptylines together 1 empty line will be missed!

-That doesn't happen in my program.




After DELETE(Number) RichEdit.Lines.Count = RichEdit.Lines.Count-1

I don't know why it becomes number on your application.. something very strange is happening on your side.

Rob ;-)
0
victor_christovCommented:
Try this

procedure TForm1.Button1Click(Sender: TObject);
Var Number : Integer ;
begin
  number := 0 ;
  with RichEdit1 do begin
    repeat
        if Trim(Lines[number]) = '' then
           Lines.Delete(number)
        else
           Inc(number) ;
    until number >= Lines.Count;
  end;
end;

It's working slowly your way, but final result is guaranteed.


0
victor_christovCommented:
You have to use Trim() only if you want to delete rows conprised of spaces only

Regards, Victor
0
karooCommented:
Masonok,

In project options, turn optimization off. Had something similar once with a downto loop. just a stupid idea from me, but like Rob said there is nothing wrong with your code.
0
RBertoraCommented:
Hey Karoo nice to see you :-)
Just wondering, do you know where the Karoo is?
Rob ;-)
0
karooCommented:
Rob,
aaaah yes, i'm origionally from South-Africa, the Karoo with it's open bare plains is one of my fav places in the world. your'e taking me back to some fond memories now:-))
0
RBertoraCommented:
Beaufortwest!
Rob ;-)
0
karooCommented:
no way!!!:))
my family used to have a farm in the Beaufortwest area, Dad went to school there (he used to say: i had to walk 15 km to school on a dirt road, hehe)
0
RBertoraCommented:
Dis a mooorse lang trap.
Rob ;-)
were you at now?
0
karooCommented:
haahaha, Ek is in die US of A, consulting, trying to get my Green Card, you?
:)
0
RBertoraCommented:
Karoo:
UK, hope you get green card, it sickens me when all these illegal buggers get across and good / hard working people are questioned to the nth degree...


Masonok,  Whats happening did turning off the optimizations as Karoo suggested work?

Rob ;-)
0
MasonokAuthor Commented:
Sorry folks! It doesn't work!
I tried Trim and tried to turned off/on an optimization! The result is SUX! After Delete(number) lines.Count became equal number -

I tried my code in the simplest program - Trichedit component and 1 Button! It works perfectly! I type a text then press the button! One moment and text is cleaned from empty lines!

But in my program it doesn't work! The program is simple - I am loading a text from text-file into TRichEdit1 and then tring to erase empty lines then I save it into the document with the same name!
I don't call any API or something wierd....

I need another suggestions!
0
victor_christovCommented:
Hi Masonok

1.If you load RichEdit form Text-only file, then you have to set
  Plain Text := True ;

2.Try this code

procedure TForm1.Button1Click(Sender: TObject);
Var Number  : Integer ;
    cMyFile : String  ;
begin
  cMyFile := { add your file name }
  with RichEdit1 do begin
       Lines.LoadFromFile(cMyFile);
       Perform(EM_LINESCROLL,0,RichEdit1.Lines.Count-1);
       Number := 0    ;
       Repeat
         if Trim(Lines[Number]) = ''
            Lines.Delete(Number);
         else
            Inc(Number) ;
       until Number >= Lines.Count;
       Lines.SaveToFile(cMyFile);
  end;
end;

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MasonokAuthor Commented:
Verrrry GOOOD!!!!!!! Excelent!

It works! It even works with RichEdit.Lines.IndexOf('') and then Delete method! :)

But your way works faster 2 or 3 times!!!

Thank you MAN!  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.