Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi: replace line breaks in a string, which is filled out of an varchar2 field (oracle), while writing a textfile

I am executing a sql-statement on a oracle database and writing the results into a textfile. A field, called "targets" (varchar2) has line breaks in it.

Until now i replaced the line breaks like this:

function CleanText(strText : String) : String;
var
  ii: integer;
begin
  for ii := 1 to Length(strText) do
    if strText[ii] < chr(32) then strText[ii] := ' ';
  end;

  Result := strText;
end;

But now i want to replace the line breaks with a specific string: "{\n}". I tried different things like:

strText := AnsiReplaceStr(strText, #13#10, '{\n}');

But it doesnt work. There a no "{\n}" in the written textfile.

I copy pasted an example string out of the sql-statement:

"1) Sicherung der Liquidität der Stadt XYZ durch:'#$A'- exakte Buchführung zur Sicherung des korrekten Ausweises der Forderungen / Verbindlichkeiten"

I could do something like this (but i think, that's not a good solution):

strText := AnsiReplaceStr(strText, '''#$A''', '{\n}');

Do you have any ideas?

Regards, Dennis

like image 898
Dennis F. Avatar asked Feb 17 '16 13:02

Dennis F.


2 Answers

Use SysUtils.AdjustLineBreaks first to normalize line breaks:

strText := AdjustLineBreaks(strText, tlbsCRLF);
strText := AnsiReplaceStr(strText, #13#10, '{\n}');
like image 95
NineBerry Avatar answered Sep 30 '22 16:09

NineBerry


Another option is to use a TStringList with its LineBreak property:

function CleanText(strText : String) : String;
var
  sl: TStringList;
begin
  sl := TStringList.Create;
  try
    sl.Text := strText;
    sl.LineBreak := '{\n}';
    Result := sl.Text;
  finally
    sl.Free;
  end;
end;
like image 37
Remy Lebeau Avatar answered Sep 30 '22 14:09

Remy Lebeau