ReplaceStrings function

pritaeas 0 Tallied Votes 261 Views Share

The ReplaceStrings function replaces all strings in APatterns found in ASubject with the value in AReplacement. As an example, I added the RemoveStrings function which replaces everthing with an empty string, thus deleting all strings found. Usage examples are in the XMLDoc comments.

A good test will be to rewrite this function to accept regular expression patterns, or write one that accepts an array of replacements, so each sub -string can be replaced with a different value.

///	<summary>
///	  Remove all patterns from the subject.
///	</summary>
///	<param name="ASubject">
///	  String to be modified.
///	</param>
///	<param name="APatterns">
///	  Sub-strings to be removed.
///	</param>
///	<example>
///	  // remove all CR and LF from newString<br />
///	  newString := RemoveStrings(oldString, [#13, #10]);
///	</example>
function RemoveStrings(const ASubject: string;
  const APatterns: array of string): string;
begin
  Result := ReplaceStrings(ASubject, APatterns, '');
end;

///	<summary>
///	  Replace all patterns in the subject.
///	</summary>
///	<param name="ASubject">
///	  String to be modified.
///	</param>
///	<param name="APatterns">
///	  Sub-strings to be replaced.
///	</param>
///	<param name="AReplacement">
///	  Replacement string.
///	</param>
///	<example>
///	  // replace all CRLF in newString with &lt;br/&gt;<br />
///	  newString := ReplaceStrings(oldString, [#13#10], '&lt;br/&gt;');
///	</example>
function ReplaceStrings(const ASubject: string;
  const APatterns: array of string; const AReplacement: string): string;
var
  i: Integer;
begin
  Result := ASubject;
  for i := Low(APatterns) to High(APatterns) do
    Result := StringReplace(Result, APatterns[i], AReplacement,
      [rfIgnoreCase, rfReplaceAll]);
end;
TrustyTony 888 pyMod Team Colleague Featured Poster

http://edn.embarcadero.com/article/32770

The XMLDoc tool requires a number of supporting tools:

Delphi 2005 or later
The XMLDoc tool. If this was not included in your copy of Delphi 2005, you can download it from Code Central.
Python (tested with Python 2.3)

Joining of my first and last languages ;)

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Cool. Didn't even know they had one. I am using DevJet's Documentation Insight for this, it includes a nice expert for viewing and editing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.