How to replace a string quickly in the whole dataset ? How to create a fast replace dialog for your app’s datasets ?
In this tutorial, we will explains the right procedure to implement a replace feature for your Delphi datasets.
The “Replace” function
It is a function for replacing an old character or string with a new given character or string.
You can use it like this example :
var
Str : String;
begin
Str := 'This is a string.';
Str := Str.Replace('a', 'one');
ShowMessage(Str);
end;
This code will show a message box with the text :
This is one string.
The “Replace” function can also have a third parameter, the “ReplaceFlags”. This parameter is introduced in order to use flags such as :
rfIgnoreCase | Ignore the case when replacing |
rfReplaceAll | Replace all occurrences of the old value in the string with the new value |
Editing the dataset records
In order to edit an active record, you have to open the dataset for editing, you make your changes (either by using the “FieldValue” property, or by calling the desired field and using its TField properties : AsString, AsInteger, …), then you post the updated record. You can also catch the exception to cancel the record’s modification.
begin
DataTable.Edit;
try
// Some code here for changing the value of some fields
DataTable.Post;
except
DataTable.Cancel;
end;
DataTable.Next;
end;
Writing a loop for replacing a string in the whole dataset
Let’s create something like a replace algorithm, to replace all occurrences of a value in a specific field in all dataset records :
- On the form (or the replace panel), drop 2 TEdit controls.
- Name them “EditOldValue” and “EditNewValue” respectively.
- Now, before setting the editing code, you should disable data display to prevent slowness, by using the procedure “DisableControls”.
- Implement the editing code inside the instruction (try … finally).
- Under the “try” keyword, start your code by positioning the dataset on the first record.
- Then, by using the (while … do) loop on all the dataset records, implement the “Replace” function within the code for editing the dataset, by using the text of those TEdit controls to call the function.
- After the “finally” keyword, enable data display, by using the procedure “EnableControls”.
Check this code snippet :
procedure TForm.ButtonReplaceClick(Sender: TObject);
var sVal : string;
begin
DataTable.DisableControls;
try
DataTable.First;
while not DataTable.Eof do
begin
DataTable.Edit;
try
sVal := DataTable.FieldByName('Field 1').AsWideString;
sVal := sVal.Replace(EditOldValue.Text, EditNewValue.Text);
DataTable.FieldByName('Field 1').AsWideString := sVal;
DataTable.Post;
except
DataTable.Cancel;
end;
DataTable.Next;
end;
finally
DataTable.EnableControls;
end;
end;
Also, you can implement the “Replace” function for as many fields as you want, using the same basis.
See also
- https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.SysUtils.TStringHelper.Replace
- https://docwiki.embarcadero.com/Libraries/Alexandria/en/Data.DB.TDataSet.Edit
- https://docwiki.embarcadero.com/Libraries/Alexandria/en/Data.DB.TDataSet.Post
- https://docwiki.embarcadero.com/Libraries/Alexandria/en/Data.DB.TDataSet.Cancel