String.Format usage

Source: Internet
Author: User

String.Format usage

Excerpt from: http://www.cnblogs.com/weichao975/archive/2011/05/12/2044724.html

Name Description
The format item in format (String, object) replaces the specified String with the text equivalent of the value of the specified Object instance.
Format (String, array<>[] () []) replaces the format item in the specified String with the text equivalent of the value of the corresponding Object instance in the specified array.
Format (IFormatProvider, String, array<>[] () []) replaces the format item in the specified String with the text equivalent of the value of the corresponding Object instance in the specified array. The specified parameter provides culture-specific formatting information.
The format item in format (String, object, object) replaces the specified string with the text equivalent of a value of two specified Object instance.
The format item in format (String, object, object, object) replaces the specified string with the text equivalent of a value of three specified Object instance.


Multiple parameters

int M[]=new Int{a,b,c,d};

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

A parameter

Private Const string _extraclause = "and C_internshiporg_internshipid = {0}";

Mycrypt.decrypt (request["id"]) as a parameter of _extraclause
if (request["id"]! = NULL && request["id"]! = string. Empty)
{
Mextramessage = string. Format (_extraclause, Mycrypt.decrypt (request["id"));
}

2. Formatted numerical results table

Character
Description
Example
Output

C currency string. Format ("{0:c3}", 2) $2.000
D decimal String. Format ("{0:d3}", 2) 002
E Scientific Counting method 1.20E+001 1.20E+001
G regular String. Format ("{0:g}", 2) 2
N the number string separated by semicolons. Format ("{0:n}", 250000) 250,000.00
X hexadecimal string. Format ("{0:x000}", "C")


String. Format ("{0:000.000}", 12.2) 012.200


String.Format is used to format a string (concatenate strings by a specified rule or output other variables and return a new string).
STRING.FORMAT (FM,...);
The first parameter uses FM to represent the output format, and each% symbol is followed by a format expression, each of which corresponds to the following parameters in order.
So with n formatted expressions, you have to add n parameters later.

The following is a simple example:

int = 123;
str = "string"

--The following%s corresponds to the parameter str,%i corresponding to the parameter int
str = String.Format ("This is the string: '%s ' This is a numeric value%i", str,int);

Win.messagebox (str);

--%05i represents a number formatted to be at least five digits, less than the preceding 0
str = String.Format ("%05i", int);
Win.messagebox (str);
Format expression:%[0 or more flags [minimum field width] [precision] [modifier] Format code

Note: [] square brackets indicate optional parameters

1. Format code

Code C
Parameter numeric value (number)
The meaning parameter is clipped to a 8-bit bytecode and printed as a character.

Code I,D
Parameter numeric value (number)
The meaning parameter is printed as a decimal integer. If the precision is given and the number of digits of the value is less than the number of precision digits, the front is filled with 0.

Code U,O,X,X
Parameter numeric value (number)
The meaning parameter is printed as an unsigned numeric value, U uses decimal, O uses octal, x or x uses hexadecimal, the difference is that the X Convention uses ABCDEF, and the X Convention uses abcdef.

Code E,e
Parameter numeric value (number)
The meaning parameter is printed according to the exponent. For example, 6.023000e23 is using code E,6.023000E23 is using code E. The number of digits after the decimal point is determined by the precision field, and the default value is 6.

Code F
Parameter numeric value (number)
The meaning parameter is printed in the general floating-point format. The Precision field determines the number of digits after the decimal point, and the default value is 6.

Code G,G
Parameter numeric value (number)
The meaning parameter is printed in the format%f or%e (such as G,%e), depending on its value. If the exponent is greater than or equal to-4 but less than the Precision field uses the%f format, otherwise the exponential format is used.

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

Code Q
Parameter (None)
Meaning prints a string and places the string in a pair of quotation marks, and automatically adds an escape character if the string contains quotation marks, and so on. If you want to read a string, pass it to the script code. To avoid special characters such as malicious quotes, you can format them using%Q.

Code
Parameter (None)
Meaning Cancel% escape print a% character, that is, a percent of the original%.

2. Logo

Flag
The meaning value is aligned in the field and is right-justified by default.

Flag 0
Meaning when the value is right-aligned, the default is to use a space to fill the left unused column. This flag indicates a 0 padding, which can be used for d,i,u,o,x,x,e,e,f,g and G codes.
When using d,i,u,o,x and X code, if the Precision field is given, the 0 flag is ignored. If a minus sign appears in the format code, the 0 flag has no effect.

Logo +
Meaning when used in a format of a signed value code, if the value is not negative, the plus sign will give it a positive number. If the value is negative, a minus sign is displayed as usual. In
By default, a plus sign is not displayed.

Flag space
The meaning is only used to convert code with signed values. When the value is non-negative, this flag adds a space to where it started. Note that this flag and the plus sign are mutually exclusive if two
At the same time, the space flag is ignored.

Flag
Meaning choose another form of conversion for some code:

Used for ... #标志 ...
o ensure that the resulting value starts with a 0
X,x prefixed with a 0x prefix of non-0 values (%x is 0X)
E,e,f ensure that the result always contains a decimal, even if there is no number behind it
The g,g is the same as the above e,e and F codes. In addition, 0 of the suffix is not removed from the small number

3. Field width

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

4. Accuracy

The precision begins with a period followed by an optional decimal number. If an integer is not given, the default value for precision is zero.

For conversions of type d,i,u,o,x and X, the Precision field specifies the minimum number of digits that will appear in the result and overrides the 0 flag. If the number of bits of the converted value is less than the width, then zero is inserted in front of it. If the value is zero and the precision is zero, the result of the conversion does not produce a number.

For conversions of type e,e and F, the precision determines the number of digits that will appear after the decimal point.

For conversions of type G and G, it specifies the maximum number of significant digits that will appear in the result.

When using the conversion of type S, the precision specifies the maximum number of characters that will be converted.

If the decimal integer that represents the width and/or precision of the field is replaced by an asterisk, then the next parameter of printf (which must be an integer) provides the width and/or precision. So
These values can be obtained by calculation without having to be specified beforehand.

5. Example

Format Code A ABC ABCDEFGH
%s A ABC ABCDEFGH
%5s # # # # #A # #ABC ABCDEFGH
%.5s A ABC ABCDE
%5.5s # # # # #A # #ABC ABCDE
%-5s 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. #0. ##### #0. XX # #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.2346e+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

Using the Format function above, it is easy to achieve the conversion of numbers.

--Converting a number to a binary string
str = String.Format ("%b", 23);

--binary string converted to digital
n = tonumber (str,2)

--Convert numbers to octal strings
str = String.Format ("%o", 23);

--eight binary string converted to digital
n = tonumber (str,8)

--Converts a number to a hexadecimal string
str = String.Format ("%x", 23);

--16 binary string converted to digital
n = tonumber (str,16)

Four, format time
The function of simulating the sprite v7.10 format time has string.time;string.ftime;os.data;os.time and other functions.
Os.time is the generation of a numeric time value from table String.time is the generation of a numeric time value from a string.
Os.data and String.ftime function the same from the time value in turn to generate a string or time.


Here we introduce the String.time function, the String.ftime function, and the following are examples of use:


--Create a time value from a string
t = String.time ("2006/6/6 0:0:0", "%y/%m/%d%h:%m:%s")

--Create a string from a time value
str = string.ftime ("%y/%m/%d%h:%m:%s", T)

Formatting syntax (syntax that is applicable in many programming languages)


%a-shorthand for the current area of the week
%A-The full name of the current area day of the week
%b-shorthand for the current region month
%B-The full name of the current region month
%c-Preferred date-time expression for the current region
%c-century value (year divided by 100 after rounding, range from 00 to 99)
%d-day of the month, decimal digits (range from 01 to 31)
%d-Same as%m/%d/%y
%e-the day of the month, the decimal number, a digit preceded by a space (range from ' 1 ' to ' 31 ')
%g--like%g, but no century
The year of the%g-4 digit, in accordance with the ISO number of weeks (see%V). The same as the%V format and value, except if the ISO week number belongs to the previous year or the following year, then the year is used.
%h-like%b,
%H-24-Hour decimal hours (range from 00 to 23)
%I-12-Hour decimal hours (range from 00 to 12)
%j-the day ordinal of the year, in decimal numbers (range from 001 to 366)
%m-Decimal month (range from 01 to 12)
%M-Decimal minutes
%n-line break
%p-based on a given time value of ' am ' or ' PM ', or the corresponding string in the current locale
%r-time with the A.M. and P.M. symbols
Time of the%r-24 hour symbol
%s-Number of decimal seconds
%t-Tab
%T-current time, same as%h:%m:%s
%u-The decimal number expression of the day of the week [1,7],1 represents Monday
%u-week of the year, starting from the first Sunday of the first week as the first day
%V-The ISO 8,601:1988 format for the week ordinal of the year, ranging from 01 to 53, 1th Week is the first of the year at least 4 days of the week, Monday as the first day of the week. (%G or%G as the number of weeks for the specified timestamp.) )
%W-Number of weeks of the year, starting from the first Monday of the first week as the first day
%w-Day of the week, Sunday for 0
%x-Preferred time notation for the current region, not including time
%x-Preferred time notation for the current region, excluding dates
%y-decimal year with no centuries (range from 00 to 99)
%Y-decimal year including the number of centuries
%Z-time zone name or abbreviation
Percent%-the '% ' character on text

String.Format usage

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.