Mega Code Archive

 
Categories / Delphi / Examples
 

Removing blocks of text

Title: Removing blocks of text Question: Never needed to remove blocks of text delimited by words or placeholders from a text? Or comments, included in brakets, from a string? Here is a usefull 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. Function RemoveBlocks( Value : String; BeginWidth : String = '('; EndWith : String = ')' ) : String; Var BeginPoint : Integer; EndPoint : Integer; Begin BeginPoint := Pos( BeginWith, Value ); EndPoint := Pos( EndWith, Value ); While ( ( BeginPoint 0 ) And ( EndPoint BeginPoint ) ) Do Begin Delete( Value, BeginPoint, ( EndPoint - BeginPoint + 1 ) ); BeginPoint := Pos( BeginWith, Value ); EndPoint := Pos( EndWith, Value ); End; Result := Value; End; For example, you can remove all paragraphs in a HTML source just using this: MyHTML := RemoveBlocks( MyHTML, '', '' ); You can also improve the function by adding a "case-insensitive" function with something like this: Function RemoveBlocks( Value : String; BeginWidth : String = '('; EndWith : String = ')' ) : String; Var BeginPoint : Integer; EndPoint : Integer; Begin BeginWith := LowerCase( BeginWith ); EndWith := LowerCase( EndWith ); BeginPoint := Pos( BeginWith, LowerCase( Value ) ); EndPoint := Pos( EndWith, LowerCase( Value ) ); While ( ( BeginPoint 0 ) And ( EndPoint BeginPoint ) ) Do Begin Delete( Value, BeginPoint, ( EndPoint - BeginPoint + 1 ) ); BeginPoint := Pos( BeginWith, LowerCase( Value ) ); EndPoint := Pos( EndWith, LowerCase( Value ) ); End; Result := Value; End;