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?
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;
Maybe add code to handle the case where delimiter is not found.
@fpiette if the delimiter is not found,
Pos()
will return 0, and soDelete(S, 1, 0)
won't delete anything. This actually works better when deleting in the left than the right, where 0 would have to be handled special to avoid clearing the whole string.thanks @Remy Lebeau