> Is there any functions in delphi for string replacing?
> I want to have a string with certain tokens that get replaced, i.e. "%user
> logged in at %now.".   I initially thought I could just use Format() and
> %s's but that means I need to always have the same amount of params in the
> string, which I don't.
> I wrote a little replace function but it keeps getting stuck in some
> recursion problems which I'm gonna look at, but I was wondering if there
> was actually any built-in functions with delphi that do this?

Nothing builtin that will do token substitution...

I have a simple one (that I'm sure could be improved considerably)...

function ExtFormat(Terminator :Char; const FormatString :String; const Params :array 
of String):String;
// Note throws away strings of length less than 3 since that's the smallest a parameter
// can be imbedded in a string.
var
  PARAM :String;
  STATE,P,iPARAM :Integer;
begin
  Result := '';
  if Terminator in ['0'..'9'] then raise Exception.Create('Terminator cannot be a 
digit');
  P := 1;
  STATE:=1;
  while P<=Length(FormatString) do begin
    case STATE of
      1: begin
        if FormatString[P]=Terminator then STATE:=2
        else Result := Result+FormatString[P];
      end;
      2: begin
        if FormatString[P]=Terminator then begin
          STATE:=1;
          Result := Result+Terminator;
        end
        else if FormatString[P] in ['0'..'9'] then begin
          STATE := 3;
          PARAM := FormatString[P];
        end
        else raise Exception.Create('Invalid character ('+FormatString[P]+') in 
FormatString parameter');
      end;
      3: begin
        if FormatString[P] in ['0'..'9'] then PARAM := PARAM+FormatString[P]
        else if FormatString[P]=Terminator then begin
          STATE := 1;
          iParam := StrToInt(PARAM);
          if iParam>High(Params) then
            raise Exception.Create('Too few parameters ('+IntToStr(Length(Params))+') 
provided')
          else Result := Result + Params[iParam];
        end
        else raise Exception.Create('Invalid character ('+FormatString[P]+') in 
FormatString parameter');
      end;
    end;
    inc(P);
  end;
  if STATE<>1 then raise Exception.Create('Unexpected termination of FormatString');
end;

This is just a quick Pushdown FSM implementation to quickly implement substitution of 
numbered
parameters in the form

'%0% is the first String passed in whilst %1% is the strnig that followed %0%. %% puts 
a percent sign in'

This is assuming that parameter terminator is '%'.

There is no reason why parameters cannot be named and enumerated from a string 
parameter passed in
and substitutions performed on strings instead of numbers - it would be less 
effificent to do a string search in
a list (even sorted to allow bin search) to find if the parameter exists though.

Does anyone know if FLEX is available for delphi... and does it produce a delphi 
Lexical analyser class rather
than spagetti. It would be handy to have as a class that handles TStreams.  NB the 
name of the Parser generator
slips my mind at the moment but I'm assuming that it too has been ported if FLEX has...

--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to