Author: Christian Cristofori
Never needed to remove blocks of text delimited by words or place holders from a
text? Or comments, included in brackets, from a string? Here is a useful function
that does that.
Answer:
Just use this function to remove text between round brackets, or specify a
different start/end placeholder to remove what you nees.
1 function RemoveBlocks(Value: string; BeginWidth: string = '('; EndWith:
2 string = ')'):
3 string;
4 var
5 BeginPoint: Integer;
6 EndPoint: Integer;
7 begin
8 BeginPoint := Pos(BeginWith, Value);
9 EndPoint := Pos(EndWith, Value);
10 while ((BeginPoint > 0) and (EndPoint > BeginPoint)) do
11 begin
12 Delete(Value, BeginPoint, (EndPoint - BeginPoint + 1));
13 BeginPoint := Pos(BeginWith, Value);
14 EndPoint := Pos(EndWith, Value);
15 end;
16 Result := Value;
17 end;
18
19 for example, you can remove all paragraphs in a HTML source just using this:
20
21 MyHTML := RemoveBlocks(MyHTML, '<', '>');
22
23 You can also improve the function by adding a "case-insensitive" function with
24 something like this:
25
26 function RemoveBlocks(Value: string; BeginWidth: string = '('; EndWith: string =
27 ')'):
28 string;
29 var
30 BeginPoint: Integer;
31 EndPoint: Integer;
32 begin
33 BeginWith := LowerCase(BeginWith);
34 EndWith := LowerCase(EndWith);
35 BeginPoint := Pos(BeginWith, LowerCase(Value));
36 EndPoint := Pos(EndWith, LowerCase(Value));
37 while ((BeginPoint > 0) and (EndPoint > BeginPoint)) do
38 begin
39 Delete(Value, BeginPoint, (EndPoint - BeginPoint + 1));
40 BeginPoint := Pos(BeginWith, LowerCase(Value));
41 EndPoint := Pos(EndWith, LowerCase(Value));
42 end;
43 Result := Value;
44 end;
|