Jump to content

[PASCAL] StringReplace function


CouldnoT
 Share

Recommended Posts

StringReplace function

 

The StringReplace function replaces characters or substrings with other characters or substrings in the desired string. 

 

program StrReplace;
uses
 cthreads,
 Classes, SysUtils;

var
 Line: string;
 Line2: string;

begin
 Line:= 'This is a test for string replacement';
 Line2:= StringReplace(Line, ' ', '-', [rfReplaceAll]);
 Writeln(Line);
 Writeln(Line2);

 Readln;
end.

 

The parameters of the StringReplace function are:


1. Line : This is the original string that we need to modify.
2. ' ' : This is the substring that we need to replace. It is space in this example.
3. '-' : This is the alternative substring that we want to replace the previous one in original string.
4. [rfReplaceAll] : This is the replacement type. In this case we need to replace all occurrence of the space substring.
 

We can use only one string variable and discard the variable Line2 as modified example shows, but we will lose the original text value:

 

var
 Line: string;
begin
 Line:= 'This is a test for string replacement';
 Writeln(Line);
 Line:= StringReplace(Line, ' ', '-', [rfReplaceAll]);
 Writeln(Line);
 Write('Press enter key to close');
 Readln;
end.

 

 

Edited by CouldnoT
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.