お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"RE^2:ファイル内文字列の検索・置き換え"

この発言は
#00425 本田勝彦 さんのRE:ファイル内文字列の検索・置き換え
に対するコメントです

ユニット使用例 ■ TSearchBufferOptions について sbMatchCase ..... 大文字小文字を区別して検索 sbIncludeCRLF .... 改行文字が含まれていてもヒットする sbIncludeSpace ... 半角・全角スペースが含まれていてもヒットする ■ ファイル内の文字列を置き換えて別ファイルへ出力し、   進行状況をプログレスバーへ表示する var Progress: Boolean = True; procedure TForm1.CancelButtonClick(Sender: TObject); begin Progress := False; end; function TForm1.ProgressProc(const StreamSize, Position:   Longint): Boolean; begin ProgressBar1.Position := Round(Position / StreamSize * 100); Application.ProcessMessages; Result := Progress; end; procedure TForm1.Button1Click(Sender: TObject); begin if ReplaceFile( 'a:\nifterm\fdelphi\room05.txt', 'a:\nifterm\fdelphi\work.txt', '謎の全知師', 'はっとりぃ', [sbMatchCase, sbIncludeCRLF, sbIncludeSpace], ProgressProc) then ShowMessage('Complete'); ProgressBar1.Position := 0; end; ■ メモ内の文字列検索 function TForm1.GetOptions: TSearchBufferOptions; var I: Integer; begin Result := []; for I := 0 to ComponentCount - 1 do if (Components[I] is TCheckBox) and (TCheckBox(Components[I]).Checked) then Result := Result + [TSearchBufferOption(Components[I].Tag)]; end; procedure TForm1.Button1Click(Sender: TObject); var Buffer, P: PChar; Info: THitPointInfo; Options: TSearchBufferOptions; begin Options := GetOptions; Buffer := Memo1.Lines.GetText; P := Buffer; try Inc(P, Memo1.SelStart + Memo1.SelLength); if SearchBuffer(Edit1.Text, P, Options, Info) then begin Memo1.SelStart := P - Buffer + Info.Start; Memo1.SelLength := Info.Length; Memo1.SetFocus; end; finally StrDispose(Buffer); end; end; ■ メモ内の文字列一括置き換え procedure TForm1.Button1Click(Sender: TObject); var Source, Dest: TMemoryStream; Info: THitPointInfo; Options: TSearchBufferOptions; begin Options := GetOptions; Source := TMemoryStream.Create; Dest := TMemoryStream.Create; try Memo1.Lines.SaveToStream(Source); Source.Position := 0; if ReplaceStream( Source, Dest, Edit1.Text, Edit2.Text, Options, nil) then begin Dest.Position := 0; Memo1.Lines.LoadFromStream(Dest); end; finally Source.free; Dest.free; end; end; 本田勝彦 Original document by 本田勝彦 氏 ID:(VYR01647)



ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum