Board index » cppbuilder » Formatting AnsiString

Formatting AnsiString

Reading the help file gave me an idea on how to format, but I am not sure
exactly.

I want to change my string from "2" to "002". Is formatting the way?

TIA,
David

 

Re:Formatting AnsiString


Quote
Michael Lockey wrote:

> David Evennou wrote:

> > Reading the help file gave me an idea on how to format, but I am not sure
> > exactly.

> > I want to change my string from "2" to "002". Is formatting the way?

> It might be; it all depends on what you want to do.

(snip)

Quote
> int k = 2;
> AnsiString F = Format("%3d", OPENARRAY (TVarRec, (k)));

A little team (not Team B) effort here.  I couldn't get it from the
Help file.

AnsiString F = Format("%-3.3d", OPENARRAY (TVarRec, (k)));

seems to work.

Paul Scott

Re:Formatting AnsiString


Quote
David Evennou wrote:

> Reading the help file gave me an idea on how to format, but I am not sure
> exactly.

> I want to change my string from "2" to "002". Is formatting the way?

It might be; it all depends on what you want to do.

In your example, it might be as simple as

AnsiString G = "2";
AnsiString K = "00" + G;
Though I suspect it's more something like wanting to zero-pad an integer
value, which is where formatting really comes in.  Lord, it's late!  and
I don't have a code sample to hand.  What you want is something like

int k = 2;
AnsiString F = Format("%3d", OPENARRAY (TVarRec, (k)));

(that's not going to put the zeros in, though.  Look it up... it's
easy...)  If you don't get it, drop me a line when I'm less tired...

Cheers
--
Michael Lockey
director, Hartlepool Systems International
note: $10.00 fee (US) for all unsolicited spam mail.  This includes all
mail that is not either specifically requested, or a personal
communication.  By sending me anything that may qualify as spam, you
have agreed to the terms and conditions of my service.  If in any doubt,
you may send me an enquiry as to whether or not your message will be
construed as Spam.  There is no charge for this service.

Re:Formatting AnsiString


Quote
David Evennou wrote:
> I want to change my string from "2" to "002". Is formatting the way?

You could try the AnsiString function 'Insert'. For example:

AnsiString aNumber="2";  aNumber.Insert("00", 1);

aNumber can now be displayed as "002"; Hope that helps.
--
Tim N.

For serious e-mail replies, remove "spl" from "splcir...@interlog.com".

Re:Formatting AnsiString


Formatting is certainly the way. One thing that I agree with Kent Reisdorph
(Teach Yourself C++ in 5 Days) about is that sprintf formatting is nicer than
AnsiString formatting and you can always switch your data back and forth
between char arrays and AnsiStrings. However the format specifiers may be
the same. To Convert '2' into '002' as a fixed length field try %03d
Quote
David Evennou wrote:
> Reading the help file gave me an idea on how to format, but I am not sure
> exactly.

> I want to change my string from "2" to "002". Is formatting the way?

> TIA,
> David

Re:Formatting AnsiString


Quote
zombacity wrote:

> the same. To Convert '2' into '002' as a fixed length field try %03d

That was my first guess also but it didn't work for me in BCB4 where
%-3.3d did work.

Paul Scott

Re:Formatting AnsiString


Would FormatFloat be of use to you :

int num = 2;
Edit1->Text=FormatFloat("000.#", num);

On Mon, 10 Apr 2000 23:56:33 -0400, "David Evennou"

Quote
<d...@data-masters.com> wrote:
>Reading the help file gave me an idea on how to format, but I am not sure
>exactly.

>I want to change my string from "2" to "002". Is formatting the way?

Wayne Gill
wayneg...@bigpond.com

Re:Formatting AnsiString


Quote
Paul Scott wrote:

> Michael Lockey wrote:

> > David Evennou wrote:

> A little team (not Team B) effort here.  I couldn't get it from the
> Help file.

LOL!

-There is some room for we lesser mortals, isn't there?
--
Michael Lockey

Re:Formatting AnsiString


Sorry; I have now checked the AnsiString format specifiers, which are in
the on-line help(BCB4), and they are a bit different to sprintf..
Quote
Paul Scott wrote:
> zombacity wrote:

> > the same. To Convert '2' into '002' as a fixed length field try %03d

> That was my first guess also but it didn't work for me in BCB4 where
> %-3.3d did work.

> Paul Scott

Re:Formatting AnsiString


Quote
> >I want to change my string from "2" to "002". Is formatting the way?

AnsiString stmp;
stmp.sprintf("%03d",2);

How's that ?

Re:Formatting AnsiString


Thanks Guys!!

Thanks to your help, I was able to use both the Insert and sprintf methods
to solve two different problems.

Cheers

David Evennou

Quote
J.Guy wrote in message <8d4vl0$f...@bornews.borland.com>...
>> >I want to change my string from "2" to "002". Is formatting the way?

>AnsiString stmp;
>stmp.sprintf("%03d",2);

>How's that ?

Re:Formatting AnsiString


Quote
"J.Guy" wrote:

> > >I want to change my string from "2" to "002". Is formatting the way?

> AnsiString stmp;
> stmp.sprintf("%03d",2);

> How's that ?

That was just answered in this thread.  "%-3.3.d" works where "%03d"
doesn't at least in BCB4.  As I mentioned "%03d" was my first guess
also.

Paul Scott

Re:Formatting AnsiString


I must be missing your post...

I wish I had known about Ansistring sprintf when I was using bcb4, but I
learned in these newsgroups that the help was missing for it in BCB4.
Please don't flame me about this, but the following line:

Quote
> AnsiString F = Format("%3d", OPENARRAY (TVarRec, (k)));

looks a lot more complicated to me than:
stmp.sprintf("%03d",2);    // even if k were substituted for the 2

Quote
"Paul Scott" <waterho...@ultrasw.com> wrote in message

news:38F6A279.F08B7F1A@ultrasw.com...
Quote
> That was just answered in this thread.  "%-3.3.d" works where "%03d"
> doesn't at least in BCB4.  As I mentioned "%03d" was my first guess
> also.

> Paul Scott

Re:Formatting AnsiString


Quote
"J.Guy" wrote:

> I must be missing your post...

No, you were ahead of me in at least one place here.  I didn't even
know that sprintf which has long been a part of C was now also a
method of AnsiString.

Quote
> I wish I had known about Ansistring sprintf when I was using bcb4, but I
> learned in these newsgroups that the help was missing for it in BCB4.
> Please don't flame me about this, but the following line:

> > AnsiString F = Format("%3d", OPENARRAY (TVarRec, (k)));

> looks a lot more complicated to me than:
> stmp.sprintf("%03d",2);    // even if k were substituted for the 2

I completely agree especially now that I know that sprintf is now also
a method of AnsiString.

The last piece is that "%03d" still doesn't seem to work in BCB4 from
AnsiString.sprintf where it does in BCB4 from the RTL (run-time
library) version of sprintf.  I presume from your previous post that
it works in BCB5 in AnsiString.sprintf.  I don't have BCB5 yet.

Quote
> "Paul Scott" <waterho...@ultrasw.com> wrote in message
> news:38F6A279.F08B7F1A@ultrasw.com...
> > That was just answered in this thread.  "%-3.3.d" works where "%03d"
> > doesn't at least in BCB4.  As I mentioned "%03d" was my first guess
> > also.

> > Paul Scott

Other Threads