String. Format usage

Source: Internet
Author: User
Tags iso 8601

1. As a parameter

 

  Name Description
  Format (String, Object) Replace the format item in the specified String with the text equivalent item of the value of the specified Object instance.
  Format (String, array <> [] () []) Replace the format item in the specified String with the text equivalent item of the value of the corresponding Object instance in the specified array.
  Format (IFormatProvider, String, array <> [] () []) Replace the format item in the specified String with the text equivalent item of the value of the corresponding Object instance in the specified array. The specified parameter specifies the format of the region.
  Format (String, Object, Object) Replace the format items in the specified String with the text equivalent items of the values of the two specified Object instances.
  Format (String, Object) Replace the format items in the specified String with the text equivalent items of the values of the three specified Object instances.

 

Multiple Parameters

Int m [] = new int {a, B, c, d };

String. format ("{0} {1} {2}", m );

One Parameter

Private const string _ extraClause = "AND C_INTERNSHIPORG_INTERNSHIPID = {0 }";

// Use MyCrypt. Decrypt (Request ["id"]) as the _ extraClause Parameter
If (Request ["id"]! = Null & Request ["id"]! = String. Empty)
{
Mexico tramessage = string. Format (_ extraClause, MyCrypt. Decrypt (Request ["id"]);
}

2. format the value result table.

 

Character

Description

Example

Output

C Currency String. Format("{0: C3}", 2) $2.000
D Decimal String. Format("{0: D3}", 2) 002
E Scientific notation 1.20E + 001 1.20E + 001
G General String. Format("{0: G}", 2) 2
N Number separated by semicolons String. Format("{0: N}", 250000) 250,000.00
X Hexadecimal String. Format("{0: X000}", 12) C
    String. Format("{0. 000}", 12.2) 012.200

 

 

String. format is used to format a string (concatenate a string based on the specified rules or output other variables and return a new string ).
String. format (fm ,...);
The first parameter uses fm to indicate the output format. Each % symbol is followed by a formatting expression, and each formatting expression corresponds to the following parameters in order.
Therefore, if N formatting expressions are used, N parameters must be added.

The following is a simple example:

Int = 123;
Str = "string"

-- The following % s corresponds to str, and % I corresponds to int.
Str = string. format ("this is a string :' % S'This is a numeric value % I", Str, Int);

Win. messageBox (str );

-- % 05i indicates the number to be formatted as at least five digits. If it is not enough, add 0 in front.
Str = string. format (" % 05i", Int);
Win. messageBox (str );

Format expression: % [zero or multiple flags] [minimum field width] [precision] [modifier] format code

Note: [] square brackets indicate optional parameters

1. format code

Code c
Number)
The description parameter is cropped to an 8-bit bytecode and printed as a character.

Code I, d
Number)
The meaning parameter is printed as a decimal integer. If the precision is given and the number of digits of the value is smaller than the number of digits of the precision, fill the front with 0.

Code u, o, x, X
Number)
Meaning parameters are printed as unsigned numeric values. u is in decimal format, and o is in octal format. x or X is in hexadecimal format. The difference between them is that x uses abcdef, X uses ABCDEF.

Code e, E
Number)
Description parameters are printed in exponential form. For example, 6.023000e23 uses code e and 6.023000E23 uses code E. The number of digits after the decimal point is determined by the precision field. The default value is 6.

Code f
Number)
Description parameters are printed in the standard floating point format. The precision field determines the number of digits after the decimal point. The default value is 6.

Code g, G
Number)
The meaning parameter is printed in the format of % f or % e (for example, G, % E), depending on its value. If the index is greater than or equal to-4 but less than the precision field, the % f format is used. Otherwise, the exponential format is used.

Code s
Parameter string value (string)
Meaning print a string.

Code q
Parameter (none)
Meaning print a string and place the string in a pair of quotation marks. If the string contains quotation marks, such as line breaks, an escape character is automatically added. If you want to read a string and pass it as the script code. To avoid malicious injection of special characters such as quotation marks, % q can be used for formatting.

Code %
Parameter (none)
Meaning cancel % escape print a % character, that is, % represents the original %.

2. Logo

Logo-
The meaning value is aligned in the field, which is right by default.

Flag 0
Meaning when the value is right aligned, it is not used to fill the left of the value with spaces by default. This flag indicates Zero Filling. It can be used for d, I, u, o, x, X, e, E, f, g, and G code.
When code d, I, u, o, x, and X are used, if an accuracy field is provided, the zero mark is ignored. If there is a negative sign in the format code, the zero sign will not work.

Flag +
Meaning when used to format a signed value code, if the value is not negative, the positive sign adds a positive sign to it. If the value is negative, a negative number is displayed as usual. In
By default, the positive signs are not displayed.

Space
The meaning is only used to convert signed values. When the value is not negative, this sign adds a space to its start position. Note that this sign and the positive sign are mutually exclusive. If two
Given at the same time, the space mark is ignored.

Flag #
Meaning select another form of conversion for some code:

Used for... # flag...
O ensure that the generated value starts with a zero
X and X are prefixed with 0x before non-zero values (0X for % X)
E, E, f make sure that the result always contains a decimal point, even if it is not followed by a number
G, G is the same as e, E, and f code above. In addition, the 0 suffix is not removed from a small number.

3. Field width

The field width is a decimal integer used to specify the minimum number of characters that will appear in the result. If the number of characters in a value is less than the width of the field, fill it in to increase the length.

4. Precision

The precision starts with a period, followed by an optional decimal number. If no integer is given, the default value of precision is zero.

For conversions of the d, I, u, o, x, and X types, the precision field specifies the minimum number that appears in the result and overwrites the zero sign. If the number of digits of the converted value is smaller than the width, insert zero before it. If the value is zero and the precision is zero, no number is generated in the conversion result.

For conversions of e, E, and f types, the precision determines the number of digits after the decimal point.

For conversions of g and G types, it specifies the maximum number of valid digits that will appear in the result.

When s type conversion is used, the precision specifies the maximum number of characters to be converted.

If a decimal integer used to indicate the field width and/or accuracy is replaced by an asterisk, the width and (or) precision of the next parameter (which must be an integer) of printf are provided. Therefore,
These values can be obtained through calculation without being specified in advance.

5. Example

Format code A ABC ABCDEFGH
% S A ABC ABCDEFGH
% 5 s #### A ## ABC ABCDEFGH
%. 5 s A ABC ABCDE
% 5.5 s #### A ## ABC ABCDE
%-5 s A #### ABC # ABCDEFGH

Format code 1-12 12345 123456789
% D 1-12 12345 123456789
% 6d ##### 1 ###-12 #12345 123456789
%. 4d 0001-0012 12345 123456789
% 6.4d ## 0001 #-0012 #12345 123456789
%-4d 1 ####-12 #12345 123456789
% 04d 0001-012 12345 123456789
% + D + 1-12 + 12345 + 123456789

Format code 1.01. 00012345 12345.6789
% F 1.000000 0.010000 0.000123 12345.678900
% 10.2d ###### 1.00 ##### 0.01 ####### 0.00 #12345.67
% E 1.000000e + 00 1.000000e-02 1.234500e-04 1.234568e + 04
%. 4e 1.0000e + 00 1.0000e-02 1.2345e-04 1.20000e + 04
% G 1 0.01 0.00012345 12345.7

Format code 6.023e23
% F 60229999999999975882752.000000
%10.2e 60229999999999975882752.00
% E 6.023000e + 23
%. 4e 6.0230e + 23
% G 6.023e + 23

 

The preceding Formatting Function allows you to easily convert numbers in hexadecimal mode.

-- Convert digits to binary strings
Str = string. format ("% B", 23 );

-- Convert a binary string to a number
N = tonumber (str, 2)

-- Convert digits to octal strings
Str = string. format ("% o", 23 );

-- Convert an octal string to a number
N = tonumber (str, 8)

-- Convert a number to a hexadecimal string
Str = string. format ("% x", 23 );

-- Convert a hexadecimal string to a number
N = tonumber (str, 16)

 

Iv. Formatting time
The formatting time functions of the simulation genie v7.10 include string. time; string. ftime; OS. data; OS. time and other functions.
OS. time is the numeric time value generated from the table string. time is the numeric time value generated from the string.
OS. data and string. ftime functions the same way from the time value to generate a string or time.


Here we will introduce the string. time Function and string. ftime function. The following is an example:


-- Create time value from string
T = string. time ("2006/6/6 0: 0", "% Y/% m/% d % H: % M: % S ")

-- Create a string from the Time Value
Str = string. ftime ("% Y/% m/% d % H: % M: % S", t)

Formatting syntax (applicable in many programming languages)


% A-abbreviation of the day of the week in the current region
% A-Full name of the current region day of the week
% B-abbreviation of the month in the current region
% B-Full name of the current region month
% C-the preferred datetime expression for the current region
% C-century value (year divided by 100 and then rounded up from 00 to 99)
% D-the day of the month, in decimal number (range: 01 to 31)
% D-the same as % m/% d/% y
% E-the day of the month, in decimal number. A space (ranging from '1' to '31') is added before a digit ')
% G-same as % G, but not Century
% G-4-digit year, which conforms to the ISO week count (see % V ). The format and value of % V are the same, except that the number of ISO weeks belongs to the previous year or the next year, the year is used.
% H-the same as % B
% H-24 hours in decimal format (range: 00 to 23)
% I-12 hours in decimal format (range: 00 to 12)
% J-the day of the year in decimal format (ranging from 001 to 366)
% M-decimal month (range: 01 to 12)
% M-decimal minutes
% N-linefeed
% P-the value is 'am' or 'pm 'based on the given time, or the corresponding string in the current region settings
% R-time with the.M. And p.m. symbols
% R-24 hour time
% S-decimal seconds
% T-Tab
% T-current time, same as % H: % M: % S
% U-the day of the week in decimal format [1, 7]. 1 indicates Monday.
% U-the week of the current year, starting from the first Sunday of the first week as the first day
% V-ISO 8601: 1988 format for the week of the current year, ranging from 01 to 53. Week 1st is the first week of the current year with at least four days, and Monday is the first day of each week. (% G or % g is used as the year of the corresponding week of the specified timestamp .)
% W-the week number of the current year, starting from the first Monday of the first week
% W-the day of the week, and Sunday is 0
% X-the preferred time representation for the current region, excluding the time
% X-the preferred time representation for the current region, excluding the date
% Y-there is no century in decimal year (range: 00 to 99)
% Y-the year in decimal format of Century
% Z-Time Zone name or abbreviation
%-Characters '%' in text

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.