Link to home
Start Free TrialLog in
Avatar of Wayne Barron
Wayne BarronFlag for United States of America

asked on

TRichEdit - Find & Replace Text at the [End] of a Line

Hello All; [Updated some information that I forgot]

 I need to look through a .txt file loaded in TRichEdit and find Text that is at the end of a "line"
And replace it.
This is going to be done to every line that is loaded in the TRichEdit.

(Updated the example below to show 2-sets of ~~)
:Example:

~Line ~~1 ~~
~Line ~~2 ~~
~Line ~~1,000 ~~

I would need to do a [Search] for just the ~~  at the "end" of each line
And then [Replace] it with some text.
(Updated: And not mess with the ~~ that is located anywhere else in the line.)

Thanks All;
Carrzkiss
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

...
var i: integer;
     s: string;
...
  for i:= 0 to Richedit1.lines.count-1 do
  begin
    s:= Richedit1.Lines[i];
    Delete(s, Length(s)-1, 2);
    Richedit1.Lines[i]:= s;
  end;

Regards
Pierre
I assumed all lines end in ~~ but if it is not the case, simply check for this before deleting i.e.

 ...
var i: integer;
     s: string;
...
  for i:= 0 to Richedit1.lines.count-1 do
  begin
    s:= Richedit1.Lines[i];
    if (s[Length(s)-1] = '~') AND (s[Length(s)-1] = '~') then
    begin
      Delete(s, Length(s)-1, 2);
      Richedit1.Lines[i]:= s;
    end;
  end;

Regards
Pierre
forgot something:

...
  s:= s + 'XY'; //or whatever you wanted to replace with
  Richedit1.Lines[i]:= s;
...
Avatar of Wayne Barron

ASKER

Thanks Pierre
I will test this code out around Lunchtime today.

Carrzkiss
Pierre

I tried out your code, but was unable to get it to work?
Could you make up a small demo and paste the code in here please?

I will be more then happy to up the points to [300] for a good solid working example.

Also, to make things a little more clearer.
The Example using the   ~~  at the end.

Characters like this will be at the end:
||
","
","","

These different characters will be replaced with a simple:
"

I am going to be using a TComboBox to store the different chracters in that are to be [Removed]
And another TComboBox to store the Characters in that are to [Replace] the removed Characters.

The TComboBox is a no brainer to implement, so the only thing that I need is just a good
Soild working example of replacing the Example that I had originally listed (or) to:

Delete  -                 ","

Replace   -               "

Take Care and Thanks for your Assistance on this.
I am going to mess around with your already supplied code, to see if I can find where the problem is.

Wayne

uses sysUtils;

...
  StringReplace(Richedit1.text,'~~'+chr(13),'"',[rfReplaceAll]);  //maybe instead of chr(13) -> chr(10)
...

meikl ;-)
Hello Meikl, Been a while.

Cannot get that to work either.
I entered it exactly as you have it, and entered the following text to work with, but nothing.

This is a test ~~
this ~ is a ~~ test ~~

---
The only thing that I am wanting to do is to [Replace] just the ~~ at the "End" of each line.
I do not want to mess with nothing else located anywhere else in the line. Just the end.
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I uped the points to [500]
Thanks Pierre.

In order to make it work for me, I have to use the [TListBox] and not my [TComboBox]
That I started out with. Which does not matter really. I just had to change up my project a little.

The code does exceptionally well thus far.
I just tested it with 5,000+ lines of text.
And it changed all the ending charcters within only about 7-seconds.

Thank you for all your time on this one.

Now, I can uninstall the other program VEdit, of which I do not care for too much.
And simply use mine. Thanks to your help.

(By the way, this little project is for personal use only within my Office)

Wayne
Glad to help.