Formatting a string

Strings, as we all know, are our primary way of interacting with the end user. Whether presented in a message box or simply directed to the Tcl shell, they need to be as fluid as possible, in the values they present. To accomplish this, Tcl provides the format command. This command allows us to format a string with variable substitution in the same manner as the ANSI C sprintf procedure. The format command is as follows:

	format string argument argument argument…

The format command accepts a string containing the value to be formatted as well as % conversion specifiers. The arguments contain the values to be substituted into the final string. Each conversion specifier may contain up to six sections—an XPG2 position specifier, a set of flags, minimum field width, a numeric precision specifier, size modifier, and a conversion character. The conversion specifiers are as follows:

Specifier

Description

d or i

For converting an integer to a signed decimal string.

u

For converting an integer to an unsigned decimal string.

o

For converting an integer to an unsigned octal string.

x or X

For converting an integer to an unsigned hexadecimal string.

The lowercase x is used for lowercase hexadecimal notations.

The uppercase X will contain the uppercase hexadecimal notations.

c

For converting an integer to the Unicode character it represents.

s

No conversion is performed.

f

For converting the number provided to a signed decimal string of the form xxx.yyy, where the number of ys is determined with the precision of six decimal places (by default).

e or E

If the uppercase E is used, it is utilized in the string in place of the lowercase e.

g or G

If the exponent is less than -4 or greater than or equal to the precision, then this is used for converting the number utilized for the %e or %E; otherwise for converting in the same manner as %f.

%

The % sign performs no conversion; it merely inserts a % character into the string.

There are three differences between the Tcl format and the ANSI C sprintf procedure:

  • The %p and %n conversion switches are not supported.
  • The % conversion for %c only accepts an integer value.
  • Size modifiers are ignored for formatting of floating-point values. See the full description of the format command in Chapter 13, the Tcl/Tk Commands section for the details on size modifiers.

How to do it…

In the following example, we format a long date string for output on the command line. Return values from the commands are provided for clarity. Enter the following command:


% set month May
May

% set weekday Friday
Friday

% set day 5
5

% set extension th
th

%set year 2010
2010

%puts [format "Today is %s, %s %d%s %d" $weekday $month $day $extension 
$year]

Today is Friday, May 5th 2010

How it works…

The format command successfully replaced the desired conversion flag delimited regions with the variables assigned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.211