问题
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