Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-21
10
Medium Priority
?
312 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:Wayne Barron
  • 5
  • 4
10 Comments
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16732361
...
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
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16732374
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
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16732381
forgot something:

...
  s:= s + 'XY'; //or whatever you wanted to replace with
  Richedit1.Lines[i]:= s;
...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Author Comment

by:Wayne Barron
ID: 16733265
Thanks Pierre
I will test this code out around Lunchtime today.

Carrzkiss
0
 
LVL 31

Author Comment

by:Wayne Barron
ID: 16735620
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

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 16739928
uses sysUtils;

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

meikl ;-)
0
 
LVL 31

Author Comment

by:Wayne Barron
ID: 16739963
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.
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 2000 total points
ID: 16740641
Note that the list of find characters is sorted and when I iterate through the list, I start from the bottom. The reason being that if you have for e.g. "," and ","," you want it to find the longer one and replace it. if you don't do it this way, it will only remove the "," part of lines ending in ",",".

Here's the demo:

DFM file:
======
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 246
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object RichEdit1: TRichEdit
    Left = 8
    Top = 8
    Width = 233
    Height = 185
    Lines.Strings = (
      'Line1||'
      'Line2","'
      'Line3","","'
      'Line4~~')
    TabOrder = 0
  end
  object GroupBox1: TGroupBox
    Left = 248
    Top = 8
    Width = 201
    Height = 185
    Caption = 'Replace characters (at end)'
    TabOrder = 1
    object lstChars: TListBox
      Left = 8
      Top = 16
      Width = 105
      Height = 129
      ItemHeight = 13
      Items.Strings = (
        '","'
        '","","'
        '||'
        '~~')
      TabOrder = 0
    end
    object btnRemove: TButton
      Left = 120
      Top = 24
      Width = 75
      Height = 25
      Caption = '&Remove'
      TabOrder = 1
      OnClick = btnRemoveClick
    end
    object inpChars: TEdit
      Left = 8
      Top = 152
      Width = 105
      Height = 21
      TabOrder = 2
    end
    object btnAdd: TButton
      Left = 118
      Top = 152
      Width = 75
      Height = 25
      Caption = '&Add'
      TabOrder = 3
      OnClick = btnAddClick
    end
  end
  object GroupBox2: TGroupBox
    Left = 456
    Top = 8
    Width = 89
    Height = 57
    Caption = 'Replace with'
    TabOrder = 2
    object cbReplace: TComboBox
      Left = 8
      Top = 24
      Width = 65
      Height = 21
      ItemHeight = 13
      TabOrder = 0
      Text = '"'
      Items.Strings = (
        '"'
        ''''
        ';'
        '}'
        'end.')
    end
  end
  object btnReplace: TButton
    Left = 456
    Top = 80
    Width = 75
    Height = 25
    Caption = 'Replace'
    TabOrder = 3
    OnClick = btnReplaceClick
  end
end


PAS File:
======
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    GroupBox1: TGroupBox;
    lstChars: TListBox;
    btnRemove: TButton;
    inpChars: TEdit;
    btnAdd: TButton;
    GroupBox2: TGroupBox;
    cbReplace: TComboBox;
    btnReplace: TButton;
    procedure btnReplaceClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure ReplaceChars(SourceList, FindChars: TStrings; ReplaceWith: string);
var i, j: integer;
    s, f, g: string;
    replace: boolean;
begin
  for i:= 0 to SourceList.count-1 do
  begin
    replace:= false;
    s:= SourceList[i];

    //check if find chars
    for j:= FindChars.Count-1 downto 0 do
    begin
      f:= FindChars[j];
      g:= copy(s, length(s)- length(f)+1, length(f));
      if (g = f) then
      begin //replace
        Delete(s, Length(s)-length(f)+1, length(f));
        s:= s + ReplaceWith;
        SourceList[i]:= s;
        Break;
      end;
    end;
  end;
end;

procedure TForm1.btnReplaceClick(Sender: TObject);
begin
  ReplaceChars(RichEdit1.Lines, lstChars.Items, cbReplace.Text);
end;

procedure TForm1.btnRemoveClick(Sender: TObject);
begin
  lstChars.DeleteSelected;
end;

procedure TForm1.btnAddClick(Sender: TObject);
begin
  if (lstChars.Items.IndexOf(inpChars.Text) <> -1) then
    raise exception.Create(Format('%s is already listed',
                                 [QuotedStr(inpChars.Text)]));
  lstChars.Items.Add(inpChars.Text);
end;

end.


Kind regards
Pierre
0
 
LVL 31

Author Comment

by:Wayne Barron
ID: 16741965
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
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16749899
Glad to help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question