掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
TStringListで、特定の文字列から開始する行を高速で抜き出す方法 (ID:39844)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
大した違いはないですよ、一応例を上げておきますね。 開始と終わりのインデックスを探して、その間をズラズラ引き抜きます。 抜き出し箇所は手抜きしてますので、改善の余地ありです。 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; ListBox1: TListBox; Edit1: TEdit; ListBox2: TListBox; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private 宣言 } fStrList:TStringList; public { Public 宣言 } end; var Form1: TForm1; implementation {$R *.dfm} procedure FindNearMin(aStrList:TStringList; S: string; var Index: Integer); var L, H, I, C: Integer; begin L := 0; H := aStrList.Count - 1; while L <= H do begin I := (L + H) shr 1; C := AnsiCompareStr(Copy(aStrList.Strings[I],1,Length(S)), S); if C < 0 then begin L := I + 1; end else begin H := I - 1; end; end; Index := L; end; procedure FindNearMax(aStrList:TStringList; S: string; var Index: Integer); var L, H, I, C: Integer; begin L := 0; H := aStrList.Count - 1; while L <= H do begin I := (L + H) shr 1; C := AnsiCompareStr(Copy(aStrList.Strings[I],1,Length(S)), S); if C <= 0 then begin L := I + 1; end else begin H := I - 1; end; end; Index := H; end; function ExtractStr(aStrList:TStringList; S:string):string; var aBegin,aEnd:Integer; i: Integer; begin FindNearMin(aStrList,S,aBegin); FindNearMax(aStrList,S,aEnd); for i := aBegin to aEnd do Result := Result + aStrList.Strings[i] + #$D#$A; end; procedure TForm1.Button1Click(Sender: TObject); begin ListBox2.Items.Text := ExtractStr(fStrList,Edit1.Text); end; procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin fStrList := TStringList.Create; for i := 0 to 1000 do fStrList.Add(IntToStr(i)); fStrList.Sort; ListBox1.Items.Text := fStrList.Text; end; end.
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.