掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
文字列置換 (ID:11275)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
>汎用的に使うのであれば、単語ずつ区切るように検索し、単語を置換するようにしてみてはどうでしょう。 やってみました。 type TWord = record IniPos:integer; Length:integer; end; TWordArray = array[0..4000] of TWord; function IsCharInWords(ch:Char):Boolean; const Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; begin result := not (StrScan(PChar(Char),ch) = nil); end; function EnumWords(const Str:string; var WA:TWordArray):integer; var i:integer; state:Boolean; begin result := 0; i := 1; state := false; while i < Length(Str)+1 do begin if IsCharInWords(Str[i]) then begin if not state then begin WA[result].IniPos := i; state := true; end; end else begin if state then begin WA[result].Length := i - WA[result].IniPos; Inc(result); state := false; end end; Inc(i); end; if state then begin WA[result].Length := i - WA[result].IniPos; Inc(result); end; end; procedure ExchangeWords(var Str:string;OldWord,NewWord:string); var WA:TWordArray; num,i:integer; begin num := EnumWords(Str,WA); for i := num-1 downto 0 do if Copy(Str,WA[i].IniPos,WA[i].Length) = OldWord then begin Delete(Str,WA[i].IniPos,WA[i].Length); Insert(NewWord,Str,WA[i].IniPos); end; end; procedure TForm1.Button1Click(Sender: TObject); var Str:string; begin Str := 'JA=PA+N+A'; ExchangeWords(Str,'A','100'); Memo1.Text := Str; end;
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.