String Format

Create a formatted string from the format and arguments provided. At run time, each format item is replaced with the string representation of the corresponding arguments.

Syntax

String.Format( string formatString, arg1, arg2, ... )

 

Parameters

formatString string The formatted string
arg1, arg2,...   Arguments to use for formatting the string.

The syntax of a formatting argument as follows

%[Flags][Width].[Precision]Specifier

Flags

0 Left-pads the number with zeroes (0)
+ Precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
- Left-justify within the given field width; Right justification is the default
space If no sign is going to be written, a blank space is inserted before the value.
#

Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values other than zero.

Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.

Used with g or G the result is the same as with e or E but trailing zeros are not removed.

Width

(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Precision

.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer.

A precision of 0 means that no character is written for the value 0.

For e, E and f specifiers: this is the number of digits to be printed after the decimal point.

For g and G specifiers: This is the maximum number of significant digits to be printed.

For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.

For c type: it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

.* The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

Specifier

The specifier is the most significant and defines the type and interpretation of the value of the corresponding argument.

Specifier Description Input Output
s String of characters String.Format ("%-12s%+10.8s", "ScanMaster", "Designer1") ScanMaster$$$$Designer
c Character String.Format("%c", 65) A
d Signed decimal integer String.Format("%+5.3d", 3.141592) $+003
i Signed integer String.Format("%+5.3i", 3.141592) $+003
f Decimal floating point String.Format("%+07.3f", 3.141592) +03.142
e Scientific notation (mantissa/exponent) using e character String.Format("%+7.3e", 314.1592) +3.142e+002
E Scientific notation (mantissa/exponent) using E character String.Format("%+7.3E", 314.1592) +3.142E+002
g Use the shorter of %e or %f String.Format("%#7.3g", 314.1592) $$$314.
G Use the shorter of %E or %f String.Format("%#7.3G", 314.1592) $$$314.
u Unsigned decimal integer String.Format("%5.3u", 314.1592) $$314
o Signed octal String.Format("%#5.3o", 314.1592) $0472
x Unsigned hexadecimal integer String.Format("%#5.3x", 314.1592) 0x13a
X Unsigned hexadecimal integer (capital letters) String.Format("%#5.3X", 314.1592) 0X13A

Note: '$' indicates a space. This will not be reflected in the actual output, it is only used for explanatory purposes.

 

Copy
Example
--This program will demonstrate the String.Format method. 
        
arg1 = "ScanMaster Designer"    
arg2 = 65    
arg3 = Math.PI    
        
--Formatted string for string type
retString1 = String.Format("%-10.6s", arg1)
--Displays "ScanMa     " 
Report(retString1)
     
    
--Formatted string for char type
retString2 = String.Format("%10c", arg2) 
--Displays "          A"
Report(retString2)
     
    
--Formatted string for float type
retString3 = String.Format("%+010.6f", arg3) 
--Displays "+03.141593"
Report(retString3)