擴充 DELPHI Pos 功能 |
|
ericyu54
一般會員 發表:1 回覆:2 積分:0 註冊:2002-04-15 發送簡訊給我 |
在 Delphi 裡有一函數為 Pos, 它負責傳回出現在字串中第一個子字串的位置, 若無則傳回 0, 但是若我有 1..n 個相同子字串要搜尋, 則 Pos 無法滿足...
我查了一下 System.pas, pos 用 Assembly 寫的...嗚呼, 只好自己寫一個 function, 野人獻曝一下...請各位先進不吝指教, 謝謝
function PosFrom(const from: Integer; const substr: ShortString; const s: AnsiString): Integer; var i, j, len: Integer; Found: boolean; begin Result := 0; if from < 1 then Result := 0 else if from > Length(s) then Result := 0 else if Length(s) < Length(substr) then Result := 0 else begin len := Length(substr); i := from; Found := False; while (not Found) and (i <= Length(s)) do for j:=1 to len do if substr[j] <> s[i] then begin inc(i, 1); break; end else begin inc(i, 1); if j = len then begin Result := i - len; Found := True; break; end; end; // end of if end; end; |
pcplayer99
尊榮會員 發表:146 回覆:790 積分:632 註冊:2003-01-21 發送簡訊給我 |
老兄, Delphi 带了一个 PosEx, 在 StrUtils.pas 里。help 里这样说:
function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer; Description PosEx returns the index of SubStr in S, beginning the search at Offset. If Offset is 1 (default), PosEx is equivalent to Pos. PosEx returns 0 if SubStr is not found, if Offset is greater than the length of S, or if Offset is less than 1. ===================引 用 文 章=================== 在 Delphi 裡有一函數為 Pos, 它負責傳回出現在字串中第一個子字串的位置, 若無則傳回 0, 但是若我有 1..n 個相同子字串要搜尋, 則 Pos 無法滿足... 我查了一下 System.pas, pos 用 Assembly 寫的...嗚呼, 只好自己寫一個 function, 野人獻曝一下...請各位先進不吝指教, 謝謝 function PosFrom(const from: Integer; const substr: ShortString; const s: AnsiString): Integer; var i, j, len: Integer; Found: boolean; begin Result := 0; if from < 1 then Result := 0 else if from > Length(s) then Result := 0 else if Length(s) < Length(substr) then Result := 0 else begin len := Length(substr); i := from; Found := False; while (not Found) and (i <= Length(s)) do for j:=1 to len do if substr[j] <> s[i] then begin inc(i, 1); break; end else begin inc(i, 1); if j = len then begin Result := i - len; Found := True; break; end; end; // end of if end; end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |