Recent

Author Topic: How to format a string with Format() and/or TStrings  (Read 4091 times)

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
How to format a string with Format() and/or TStrings
« on: September 01, 2014, 08:21:17 pm »
mangakissa advised me to use the function format and TStrings to split my data nicely on a line, but I meet some challenges!  :o
If I write:
Code: [Select]
HeaderTekst:= Format ('[%s]',['Land']);
I can then make a
Code: [Select]
Memo1.Lines.Add(HeaderTekst);
It is working, but I still have the brackets

Now if I write
Code: [Select]
HeaderTekst:= Format ('[%s]',['Land']);
HeaderTekst := HeaderTekst + Format ('[%50:s]',['Post']);
Memo1.Lines.Add(HeaderTekst);
Then it is going not well!  %)

What I want to do is splitting the following line, so the elements are aligned on my TMemo:
Memo1.Lines.Add(MyReport[i,0] + '   ' + MyReport[i,1]) +'   '+ MyReport[i,2]+ MyReport[i,3] + '  '+ MyReport[i,4] + '  '+ MyReport[i,5] + '  '+ MyReport[i,6] + '  '+ MyReport[i,7] + '  '+ MyReport[i,8] + '  '+ MyReport[i,9] + '  '+ MyReport[i,10] + '  '+ MyReport[i,11] + '  '+ MyReport[i,12] + '  ');

If you know some reading or tutorial, I am intersted. Until now I found this one: http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/format.html
Not bad at all, but not enough for a "dummy" like me!

Best Regards
Jean-Christophe

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to format a string with Format() and/or TStrings
« Reply #1 on: September 01, 2014, 09:37:38 pm »
Code: [Select]
HeaderTekst:= Format ('[%s]',['Land']);
I can then make a
Code: [Select]
Memo1.Lines.Add(HeaderTekst);
It is working, but I still have the brackets
Remove them:
Code: [Select]
HeaderTekst:= Format ('%s',['Land']);

Code: [Select]
HeaderTekst:= Format ('[%s]',['Land']);
HeaderTekst := HeaderTekst + Format ('[%50:s]',['Post']);
Memo1.Lines.Add(HeaderTekst);
Then it is going not well!  %)
The width (50) should go after the colon:
Code: [Select]
HeaderTekst := HeaderTekst + Format ('[%:50s]',['Post']);

What I want to do is splitting the following line, so the elements are aligned on my TMemo:
Memo1.Lines.Add(MyReport[i,0] + '   ' + MyReport[i,1]) +'   '+ MyReport[i,2]+ MyReport[i,3] + '  '+ MyReport[i,4] + '  '+ MyReport[i,5] + '  '+ MyReport[i,6] + '  '+ MyReport[i,7] + '  '+ MyReport[i,8] + '  '+ MyReport[i,9] + '  '+ MyReport[i,10] + '  '+ MyReport[i,11] + '  '+ MyReport[i,12] + '  ');
I don't know if this is what you want:
Code: [Select]
var
  iWidth: integer;
  FmtStr: String;
  NumberOfValues, i:integer;
begin
  //1- Build a suitable formatting string
  iWidth := 20;     //<---- change the width to what you want
  NumberOfValues := 12;   //<--- make sure this number is correct

  for i := 0 to NumberOfValues - 1 do
    FmtStr := FmtStr + Format('%%%d:%ds',[i, iWidth]);

  //1-1- You might want to see the formatting string?
  //Memo1.Lines.Add(FmtStr);

  //2- Use it
  Memo1.Lines.Add(Format(FmtStr,[MyReport[i,0], MyReport[i,1], MyReport[i,2], MyReport[i,3], MyReport[i,4], MyReport[i,5], MyReport[i,6], MyReport[i,7], MyReport[i,8], MyReport[i,9], MyReport[i,10], MyReport[i,11], MyReport[i,12]]));

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: How to format a string with Format() and/or TStrings
« Reply #2 on: September 02, 2014, 07:12:04 am »
Thank you for your answer engkin!  :)
I will try this as soon as possible!

Have a nice day
Best Regards
Jean-Christophe

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: How to format a string with Format() and/or TStrings
« Reply #3 on: September 02, 2014, 10:26:08 am »
Hello Engkin!

I tried and it works fine, but there is still something I don't understand:

Code: [Select]
Format('%%%d:%ds',[i, iWidth])
i is the number of my array of string. ok
iWidth is the "at least" place for the container. ok

But I don't understand the 2 other lines:
%%%d
%ds
In the link I have in my problem definition, d is used for integer, so I am a little bit confused!  :o

Best Regards
Jean-Christophe

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to format a string with Format() and/or TStrings
« Reply #4 on: September 02, 2014, 11:48:22 am »
Hello Engkin!

I tried and it works fine, but there is still something I don't understand:

Code: [Select]
Format('%%%d:%ds',[i, iWidth])
i is the number of my array of string. ok
iWidth is the "at least" place for the container. ok

But I don't understand the 2 other lines:
%%%d
%ds
In the link I have in my problem definition, d is used for integer, so I am a little bit confused!  :o

Best Regards
Jean-Christophe
% is an escape character in order to include it in the final string with out the format function misinterpreting it you type it twice.

so the first loop will create a format string using format as the string builder this means that the final string string will have the form %x:ys where x nad y are constant numbers (ee 20,12 etc) which is what you need to lookup in your help file.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: How to format a string with Format() and/or TStrings
« Reply #5 on: September 02, 2014, 02:28:43 pm »
small example  ;)
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to format a string with Format() and/or TStrings
« Reply #6 on: September 03, 2014, 12:28:54 am »
But I don't understand the 2 other lines:
%%%d
%ds
Format('%%%d:%ds',[i, iWidth]) should produce:
Percentage mark (%)
followed by the value of (i)
then a colon ( : )
next the value of (iWidth)
and finally s

For instance, if i = 5 and iWidth = 20 then:
Format('%%%d:%ds',[i, iWidth]) would be:
%5:20s

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: How to format a string with Format() and/or TStrings
« Reply #7 on: September 03, 2014, 06:59:01 pm »
Thank you for all you answers!
Now I have something to work from!  :D

As I am working on the database side of my program and the weather is really nice in DK right now, it wil take a little while, before I can get back to you...

I really appreciate your help!

Best Regards
Jean-Christophe


 

TinyPortal © 2005-2018