取代有规律的字符

 

function SplitStr(pSource: string; OldStr: string; SpliChar: Char): string;
var
  s: string;
  strList: TStrings;
  i: Integer;
  iPos: Integer;
  sPos: string;
begin
  Result := '';
  strList := TStringList.Create;

  s := StringReplace(pSource, OldStr, SpliChar, [rfReplaceAll, rfIgnoreCase]);
  ExtractStrings([SpliChar], [], PChar(s), strList);
  for i := 0 to strList.Count - 1 do
  begin
    sPos := strList.Strings[i];
    if sPos[1] = '''' then {如果第一个字符为 ' }
    begin
      Delete(sPos, 1, 1); { 删除第一个 ' }
      iPos := Pos('''', sPos);
      if iPos <> 0 then { 查找第二个 ' }
      begin
        sPos := '''%' + StringReplace(sPos, '''', '%''', []);
        sPos := OldStr + sPos; {还原 'like ' 这个字符}
      end;
    end;
    Result := Result + sPos;
  end;


  strList.Free;{出来混总是要还的}
end;

{调用例子}
begin
  Edit2.Text := SplitStr(Edit1.Text, 'like ', '#');
end;

 

 

posted on 2010-07-08 11:05  思想。生活。网络  阅读(137)  评论(0编辑  收藏  举报

导航