Reading from file FreePascal

这一生的挚爱 提交于 2019-12-13 02:31:06

问题


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:


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;


来源:https://stackoverflow.com/questions/15447535/reading-from-file-freepascal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!