Inno Setup - Pad a string to a specific length with zeros

青春壹個敷衍的年華 提交于 2021-02-08 10:27:24

问题


Here's what my code currently looks like:

var
  Page: TInputQueryWizardPage;

procedure IDKeyPress(Sender: TObject; var Key: Char);
var
  KeyCode: Integer;
begin
  KeyCode := Ord(Key);
  if not ((KeyCode = 8) or ((KeyCode >= 48) and (KeyCode <= 57))) then
    Key := #0;
end;

Procedure InitializeWizard();
Begin
Page := CreateInputQueryPage(blahblah);
  Page.Add('Profile ID:', False);
  Page.Edits[0].MaxLength := 16;
  Page.Edits[0].OnKeyPress := @IDKeyPress;
  Page.Values[0] := '0000000000000000';
End;

procedure WriteUserInput;
var
A: AnsiString;
U: String;
begin
    LoadStringFromFile(ExpandConstant('{app}\prefs.ini'), A);
    U := A;
    StringChange(U, '0000000000000000', Page.Values[0]);
    A := U;
    SaveStringToFile(ExpandConstant('{app}\prefs.ini'), A, False);
end;

procedure CurStepChanged(CurStep: TSetupStep);
Begin
if  CurStep=ssPostInstall then
  begin
    WriteUserInput;
  end
End;

Now what I need Inno to do is to leave the user input as it is, if it's already 16 digits, of fill with 0's at end, if it's less than 16 (e.g only one 0 if it's 15 digits, two if it's 14, etc.). What function would be able to do that?


回答1:


A generic function for right-padding a string to a certain length with a given character:

function PadStr(S: string; C: Char; I: Integer): string;
begin
  Result := S + StringOfChar(C, I - Length(S));
end;

For your particular need, use:

StringChange(U, '0000000000000000', PadStr(Page.Values[0], '0', 16));

If you need the padding on compile-time (in the preprocessor), see How to pad version components with zeroes for OutputBaseFilename in Inno Setup.



来源:https://stackoverflow.com/questions/34698256/inno-setup-pad-a-string-to-a-specific-length-with-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!