Mega Code Archive

 
Categories / Delphi / Forms
 

ASP.NET alike string.format function for Delphi

Title: ASP.NET alike string.format function for Delphi Question: Really dislike the format function within delphi, I prefer the string.format function within ASP.NET so I build the delphi version of it. enjoy Ronald Answer: //////////////////////////////////////////////////////////////////////////////////// // Procedure - StringFormat // Author - RB // Date - 18-Feb-2004 // Remarc - ASP.NET like string.format function // // Returns a string containing a string representation formatted with all the array // elements found in the source string. // // Better solution than the basic format function within delphi. // // eg. StringFormat('This is a {1}, numbers {0},{2}, boolean {3}',[1,'test',5.5,false]) // //////////////////////////////////////////////////////////////////////////////////// function StringFormat ( const value : string; const items : array of variant ) : string; var Ts,Ts1 : String; i,j : integer; begin Ts := Value; for i := low(items) to high(items) do begin Ts1 := items[i]; j := pos(format('{%d}',[i]),Ts); while j 0 do begin system.delete(ts,j,3); system.insert(ts1,ts,j); j := pos(format('{%d}',[i]),Ts); end; end; Result := Ts; end; procedure test; begin showmessage(StringFormat('This is a {1}, numbers {0},{2},{0}, boolean {3}',[1,'test',5.5,false])); end;