Details about the format function in Delphi

Source: Internet
Author: User
Format is a very common but annoying method. I try to translate the help of this method to give it a complete overview for you to query:

First, let's look at its statement:
Function Format (const format: string; const ARGs: array of const): string; overload;

In fact, the format method has two forms, and the other is three parameters. The main difference is that it is thread-safe,
But it is not widely used, so here we will only introduce the first one:
Function Format (const format: string; const ARGs: array of const): string; overload;
 
The format parameter is a format string used to format the values in args. What is ARGs,
It is a variant array, that is, it can have multiple parameters, and each parameter can be different.
For example:

Format ('My name is % 6s ', ['wind']);
My name is wind.

Now let's take a look at the details of the format parameter:
In the format, you can write common strings, such as 'My name is ', but some command characters in the format have special meanings. For example, the format commands in the "% 6 s" format have the following forms:
"%" [Index ":"] ["-"] [width] ["." prec] Type
It starts with "%", and

Type. Needless to say.
X must be an integer value, which is returned in hexadecimal format.
Edit1.text: = format ('this is % x', [15]);
The returned result is: This is F.

After the type description is completed, the following describes the instructions for formatting the type:
[Index ":"] How to express this? Let's look at an example.
Format ('this is % d % d', [12, 13]);
The index of the first % d is 0, and the second % d is 1, so this is the case when the characters are displayed. This is 12 13.

If you define it as follows:
Format ('this is % 1: d % 0: d', [12, 13]);
Then the returned string is changed to this is 13 12. Do you understand now? The index in [Index ":"] indicates the order in which parameters are displayed in args. If so
Format ('% d % 0: d % d', [1, 2, 3, 4])
1 2 3 1 2 is returned.

If you want to return 1 2 3 1 4, you must set it as follows:
Format ('% d % 0: d % 3: d', [1, 2, 3, 4])

Note that the index cannot exceed the number in args. Otherwise, an exception occurs, as shown in figure
Format ('this is % 2: d % 0: d', [12, 13]);
Because ARGs has only 12 13 numbers, the index can only be 0 or 1. If it is 2, [width] indicates the width of the value to be formatted, let's see an example.

Format ('this is % 4D '[12]);
The output is: This is 12, which is relatively easy. However, if the width value is smaller than the length of the parameter, it will not work.
For example:

Format ('this is % 1D ', [12]);
Output: This is 12

["-"] This specified parameter is aligned to the left, and is combined with [width] to see the effect:
Format ('this is %-4d, yes ', [12]);
Output: This is 12, yes

["." Prec] specifies the precision, which has the best effect on floating point numbers:
Format ('this is %. 2f ', ['1. 1234]);
Output this is 1.12
Format ('this is %. 7f', ['1. 1234]);
Output this is 1.1234000

For integer data, if the prec value is smaller than the integer value, the result is no greater than the integer value. If the prec value is smaller than the integer value, the value is supplemented with 0 before the integer value.
Format ('this is %. 7D ', [1234]);
Output: This is 0001234]

For the character type, it is just the opposite of the integer value. If the prec type is larger than the string type, it is ineffective. If the prec type is smaller than the string type, the character at the end is truncated.
Format ('this is %. 2s ', ['123']);
The output is this is 12.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.