:TITLE: Modifying Strings - tolower, toupper, trim, format ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl19.lsn,v 1.1 2004/11/04 16:01:13 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: These are the commands which modify a string. None of these modify the string in place.
format man page.
A useful subset of the definition is that formatString consists of
literal words, backslash sequences, and % fields. The % fields
are strings which start with a % and end with one of:
string command also includes several commands which
will modify a string. When one of these commands is used, it returns a new
modified string, and leaves the original string intact. If you wish to
replace the original string you can assign the new string to the original
string name.
Tcl also includes a command that will take data from several sources and
create a new string. The format command simplifies the
creation of strings with a particular amount of data formatted in a
particular way.
The full definition of this protocol is in the format man page.
A useful subset of the definition is that formatString consists of
literal words, backslash sequences, and % fields. The % fields
are strings which start with a % and end with one of:
The formatString argument to the format command needs
some further explanation.
A simple format string would be just "%s". This would print out the arg as a simple string. ie:
set xx [format "%s" "This is a test"] and set xx "This is a test"are equivalent statements.
The format command is more useful when you are building tables, and want each entry to fill the same number of spaces, or when you are dealing with floating point numbers and want to control the precision that is reported.
The example below shows the format command being used to
create a simple table. The string %-20s places the first
arg in a 20 space field, and puts the first character of arg
in the lefttmost of the 20 spaces. This is called left justified.
The string %10s puts the second arg in a field 10
spaces long, with the last character in the arg in the rightmost
space of the field. This is right justified, and is the default
mode for strings.
The string %10d treats the second arg as an integer,
and right justifies it.
In the last format statement, the %10.2f formats the arg with 10 spaces, 2 of which are to the right of the decimal. Note that the Pentium style price is rounded to the nearest cent. :TEXT_END: :CODE_START: set upper "THIS IS A STRING IN UPPER CASE LETTERS" set lower "this is a string in lower case letters" set trailer "This string has trailing dots ...." set leader "....This string has leading dots" set both "((this string is nested in parens )))" puts "tolower converts this: $upper" puts " to this: [string tolower $upper]\n" puts "toupper converts this: $lower" puts " to this: [string toupper $lower]\n" puts "trimright converts this: $trailer" puts " to this: [string trimright $trailer .]\n" puts "trimleft converts this: $leader" puts " to this: [string trimleft $leader .]\n" puts "trim converts this: $both" puts " to this: [string trim $both "()"]\n" set labels [format "%-20s %+10s " "Item" "Cost"] set price1 [format "%-20s %10d Cents Each" "Tomatoes" "30"] set price2 [format "%-20s %10d Cents Each" "Peppers" "20"] set price3 [format "%-20s %10d Cents Each" "Onions" "10"] set price4 [format "%-20s %10.2f per Lb." "Steak" "3.59997"] puts "\n Example of format:\n" puts "$labels" puts "$price1" puts "$price2" puts "$price3" puts "$price4" :TEXT_END: