文本文件查找字符串问题(50分)

  • 主题发起人 主题发起人 wglifs
  • 开始时间 开始时间
W

wglifs

Unregistered / Unconfirmed
GUEST, unregistred user!
一个文本文件不能作为表来处理但它是有规律的,用POS函数来查找指定的字符串,但文本文件中可能在多处存在这个字符串,我怎样查找最后一个?或者是从最后一个开始查找?
 
1.如果文件比较小的话,先用 StrUtils 单元的 ReverseString 函数反转字符串再用 Pos;<br>2.这是我根据 StrUtils 单元的 PosEx 查找函数改编的反方向 Pos 函数<br>function TForm1.PosReverse(const SubStr, S: String): Integer;<br>var<br>&nbsp; I,X: Integer;<br>&nbsp; Len, LenSubStr: Integer;<br>begin<br>&nbsp; LenSubStr := Length(SubStr);<br>&nbsp; Len := Length(S) - LenSubStr + 1;<br>&nbsp; i := Len;<br>&nbsp; while I &gt; 0 do begin<br>&nbsp; &nbsp; if S = SubStr[1] then begin<br>&nbsp; &nbsp; &nbsp; X := 1;<br>&nbsp; &nbsp; &nbsp; while (X &lt; LenSubStr) and (S[I + X] = SubStr[X + 1]) do<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(X);<br>&nbsp; &nbsp; &nbsp; if (X = LenSubStr) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := I;<br>&nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Dec(I);<br>&nbsp; end;<br>&nbsp; Result := 0;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Caption := IntToStr(PosReverse('11','11223311445'));<br>end;
 
var<br>&nbsp; TmpList :TSTringList;<br>&nbsp; i : integer;<br>begin<br>&nbsp; TmpList := TStringList.Create;<br>&nbsp; TmpList.LoadFromFile('c:/file.txt');<br>&nbsp; for i := TmpList.Count -1 downto 0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if Pos('abc',TmpList) &gt; 0 then Break;<br>&nbsp; end;<br>&nbsp; ShowMessage('找到了第'+IntToStr(i)+'行是最后一个');<br>&nbsp; TmpList.Free;<br>end;<br><br>//我的只是找每行的第一个符合条件的字符串,换成楼上的方法就行了。
 
谢谢李翔鹏及liyinwei
 
后退
顶部