Reading from file FreePascal

2019-08-04 08:53发布

So I have text file containing:

Harry Potter and the Deathly Hallows###J. K. Rowling###2007

And I have to output it to the FreePascal program in the following form

J.K.Rowling "Harry Potter and the Deathly Hallows" 2007 year

I know how to read from file but I don't know how to make it like in the previos form

Can someone help me? I would be very thankful.

1条回答
贪生不怕死
2楼-- · 2019-08-04 09:36

If TStringList in freepascal is the same as in Delphi, then this would do the trick:

function SortedString( const aString : String) : String;
var
  sList : TStringList;
begin
  Result := '';
  sList := TStringList.Create;
  try
    sList.LineBreak := '###';
    sList.Text := aString;
    if (sList.Count = 3) then
    begin
      Result := sList[1] + ' "' + sList[0] + '" ' + sList[2] + ' year';
    end;
  finally
    sList.Free;
  end;
end;

Update, as commented by @TLama, freepascal TStringList does not have a LineBreak property.

Try this instead (using ReplaceStr in StrUtils):

function SortedString(const aString : String) : String;
var
  sList : TStringList;
begin
  Result := '';
  sList := TStringList.Create;
  try 
    sList.Text := ReplaceStr(aString,'###',#13#10);
    if (sList.Count = 3) then
    begin
      Result := sList[1] + ' "' + sList[0] + '" ' + sList[2] + ' year';
    end;
  finally
    sList.Free;
  end;
end;
查看更多
登录 后发表回答