Warm tip: This article is reproduced from serverfault.com, please click

Delete multiple string left in [Delphi]

发布于 2020-11-28 17:42:43

Hello I need a simple function to delete left text strings, see the example below:

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
begin
  S := 'Hello test test test [delimitator] goodby.. test teest test';
  Delete(S, Pos('[delimitator]', S), MaxInt);
  RichEdit1.Text := S;
end;

This function clears all the characters on the right, even if I don't know how many characters there are.

The question is, how to do the reverse, to delete all the characters on the left?

Questioner
delphiticks
Viewed
0
Remy Lebeau 2020-11-29 01:47:06

You already know what to use - Pos() and Delete(). Just tweak how you use them:

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
begin
  S := 'Hello test test test [delimitator] goodby.. test teest test';
  Delete(S, 1, Pos('[delimitator]', S));
  RichEdit1.Text := S;
end;