Fixing the length of an integer's string value

Respektable
Respektable used Ask the Experts™
on
Ok. so math was never my strong suit. Or maybe just my dumbness is showing again.... but

I need to save the index value of a TCollectionItem index value to a string of a preset length (so I can later sort the values after reading from stream).

What is the easiest way to ensure that the string is "N" characters long.

eg.
indexValue = 5;

desired string is:  00000005.

Is there some trick I can use, or do I have to manually pad the string with 0s after testing length?  I know that will work, but I have this nagging feeling I am missing the obvious.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
var s : string;
begin
  s := intToStr(IntegerVar);
  while length(s) < YourWantedLength do
    s := '0'+s;
  //now do what with s
end;

meikl ;-)
Software Engineer, Advisory
Top Expert 2005
Commented:

You could use the Format function, replace the 8 in the '%-.d' with the length of the desired string.

var
 i: Integer;
 s: String;
begin

i:=100;
s:=Format('%-.8d', [i]);

end;

---

Russell

Author

Commented:
Ah... thank you both.

I had already done kretz's solution. That was the padding solution.  Russel has the function I was looking for!!!

I love this service!

Regards!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial