如何从字符串中一行一行读(30分)

  • 主题发起人 主题发起人 l0v3_y1n9
  • 开始时间 开始时间
L

l0v3_y1n9

Unregistered / Unconfirmed
GUEST, unregistred user!
如果是一个比较长的字符串,里面含有回车换行...<br>而Readln需要文件变量才可以读,那么如何实现在字符串里一行一行读? &nbsp;[:D]
 
Memo1.lines.string(i);
 
如果这个字符串保存在文件中,则一个一个字符读取,读取到回车时候换一行。
 
var<br>&nbsp; TmpList : TStringList;<br>&nbsp; i : integer;<br>begin<br>&nbsp; TmpList := TStringList.Create;<br>&nbsp; TmpList.LoadfromFile('c:/aa.txt');<br>&nbsp; for i := 0 to TmpList.count - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; TmpList.strings; //当前行的值<br>&nbsp; end;<br>end;
 
用类实在太XX了...<br>难道没有纯洁的?[8D]
 
Function getStrings(var LongS : String):TStringlist;<br>var i:integer;<br>begin<br>&nbsp; Result := TStringlist.Create;<br>&nbsp; while LongS &lt;&gt; '' do<br>&nbsp; begin<br>&nbsp; &nbsp; i := pos('#',LongS);<br>&nbsp; &nbsp; if i &lt;= 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result.Add(LongS);<br>&nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if strtoint(copy(LongS,i+1,2)) = 13 then<br>&nbsp; &nbsp; &nbsp; Result.Add(copy(LongS,1,i+2));<br>&nbsp; &nbsp; LongS := copy(LongS,i+3,length(Longs));<br>&nbsp; end;<br>end;
 
function GetNextLineData(var Ptr: PChar; var Size; Integer; var S: string): Boolean;<br><br>&nbsp; procedure IncPtr;<br>&nbsp; begin<br>&nbsp; &nbsp; Inc(Ptr);<br>&nbsp; &nbsp; Dec(Size);<br>&nbsp; end;<br><br>var<br>&nbsp; Src: PChar; &nbsp;<br>begin<br>&nbsp; // 忽略空行<br>&nbsp; while Ptr^ in [#13, #10] do IncPtr; &nbsp; <br>&nbsp; Result := Size &gt; 0;<br>&nbsp; // 起始<br>&nbsp; Src := Ptr;<br>&nbsp; // 扫描到行尾<br>&nbsp; while not (Ptr^ in [#13, #10, #0]) do IncPtr;<br>&nbsp; // 长度 = 行尾-起始<br>&nbsp; SetString(S, Src, Ptr - Src);<br>&nbsp; Result := Ptr - Src &gt; 0;<br>&nbsp; // 当前行的crlf字符,忽略掉<br>&nbsp; if Ptr^ = #13 then IncPtr;<br>&nbsp; if Ptr^ = #10 then IncPtr;<br>end;<br><br>var<br>&nbsp; SourceData, LineData: string;<br>&nbsp; SrcPtr: PChar;<br>&nbsp; SrcLen: Integer;<br>begin<br>&nbsp; SourceData := 'your long string data';<br>&nbsp; SrcPtr := PChar(SourceData);<br>&nbsp; SrcLen := Length(SourceData);<br>&nbsp; while GetNextLineData(SrcPtr, SrcLen, LineData) do<br>&nbsp; begin<br>&nbsp; &nbsp; // do your code...<br>&nbsp; end;<br>end;
 
感谢各位的热心解答,分不算多,难以报答大家.<br>我感觉大富翁越来越冷清了,希望大家坚守此地~ [8D]
 
后退
顶部