unit ANBFormatString;
{$MODE Delphi}
{$codepage UTF8}
interface
uses StrUtils, SysUtils, VCLEx;
{stdcalls}
function FormatStr(const AFrmtdStr: String; const AArgs, AArgsValues: array of String): String; STDCALL;
function FormatStrW(const AFrmtdStr: WideString; const AArgs, AArgsValues: array of WideString): WideString; STDCALL;
implementation
function SearchAndReplaceStr(const AStr, ASearchStr, AReplaceStr: String): String;
var CurrDelim, NextDelim: IntEx;
    ElemName, s: String;
begin
s:= '';
CurrDelim:= 1;
repeat
 NextDelim:= PosEx(ASearchStr, AStr, CurrDelim);
 if NextDelim = 0 then
  NextDelim:= Length(AStr) + 1;
 ElemName:= Copy(AStr, CurrDelim, NextDelim - CurrDelim);
 if not (NextDelim > Length(AStr)) then
  s:= s + ElemName + AReplaceStr
  else
  s:= s + ElemName;
 CurrDelim:= NextDelim + Length(ASearchStr);
 until CurrDelim > Length(AStr);
Result:= s;
end;
{Only for Windows}
function SearchAndReplaceStrW(const AStr, ASearchStr, AReplaceStr: WideString): WideString;
var CurrDelim, NextDelim: IntEx;
    ElemName, s: WideString;
begin
s:= '';
CurrDelim:= 1;
repeat
 NextDelim:= PosEx(ASearchStr, AStr, CurrDelim);
 if NextDelim = 0 then
  NextDelim:= Length(AStr) + 1;
 ElemName:= Copy(AStr, CurrDelim, NextDelim - CurrDelim);
 if not (NextDelim > Length(AStr)) then
  s:= s + ElemName + AReplaceStr
  else
  s:= s + ElemName;
 CurrDelim:= NextDelim + Length(ASearchStr);
 until CurrDelim > Length(AStr);
Result:= s;
end;
function FormatStr(const AFrmtdStr: string; const AArgs, AArgsValues: array of String): String;
var i: IntEx;
    s: String;
begin
s:= AFrmtdStr;
if High(AArgs) <> High(AArgsValues) then
 raise Exception.Create('Array of arguments not equal array of its values!');
for i:= 0 to High(AArgs) do
 s:= SearchAndReplaceStr(s, AArgs[i], AArgsValues[i]);
Result:= s;
end;
{Only for Windows}
function FormatStrW(const AFrmtdStr: WideString; const AArgs, AArgsValues: array of WideString): WideString;
var i: IntEx;
    s: WideString;
begin
s:= AFrmtdStr;
if High(AArgs) <> High(AArgsValues) then
 raise Exception.Create('Array of arguments not equal array of its values!');
for i:= 0 to High(AArgs) do
 s:= SearchAndReplaceStrW(s, AArgs[i], AArgsValues[i]);
Result:= s;
end;
end.
