Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing strings containing variable names in Delphi

I'm currently writing an application in which I have a function as follows:

var a,b,c, algorithm: string
begin

a := some-operations-with-regular-expressions;
b := some-other-operation-with-regular-expressions;
c := just-similar-to-b-but-different;

algorithm := IniFile.ReadString('XML Info','AlgorithmExpression', ''); 
//algorithm is fetched like 'http://wwww.urlhere' + a + '/' + b + '/' + c
DoDownload (algorithm, true);
end;

Now what I expected was to have a, b & c automatically replaced with the values of the variables with the same name, but it appears that I'm so wrong. Is there any way to get the result of the algorithm variable composed of the strings between '' and the variable values?

Any suggestion (even if it calls for major redesigning) would be much appreciated.

Thanks, sphynx

like image 566
Bogdan Botezatu Avatar asked Dec 05 '22 19:12

Bogdan Botezatu


2 Answers

I didn't understand what you are trying to do, but here goes a shot.

IniFile should contain something like this:

[XML Info]
AlgorithmExpression=http://wwww.urlhere[<a>]/[<b>]/[<c>]

and you could do something like this:

algorithm := IniFile.ReadString('XML Info','AlgorithmExpression', ''); 
algorithm := StringReplace(algorithm,'[<a>]',a,[]); 
algorithm := StringReplace(algorithm,'[<b>]',b,[]); 
algorithm := StringReplace(algorithm,'[<c>]',c,[]); 
DoDownload (algorithm, true);
like image 127
Christian Avatar answered Dec 31 '22 14:12

Christian


If this is about formatting, Delphi also has the C style format command:

var
  output: String;
begin
  output := Format('http://%s/%s/%s', ['this', 'that', 'otherthing']);
  ShowMessage(output);
end;

Shows the message:

http://this/that/otherthing

like image 45
Marcus Adams Avatar answered Dec 31 '22 12:12

Marcus Adams