《TCL 教程英文版》 笔记 §21 修改字符串- tolo..
时间:2008-08-27 来源:oychw
§21 修改字符串- tolower, toupper, trim, format
string tolower string
大写转换位小写
string toupper string
小写转换位大写
string trim string ?trimChars?
去掉指定字符,默认是空白内容(spaces, tabs, newlines)。
string trimleft string ?trimChars?
string trimright string ?trimChars?
format formatString ?arg1 arg2 ... argN?
Returns a string formatted in the same manner as the ANSI sprintf procedure. FormatString is a description of the formatting to use. 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:
· s... Data is a string
· d... Data is a decimal integer
· x... Data is a hexadecimal integer
· o... Data is an octal integer
· f... Data is a floating point number
The % may be followed by:
· -... Left justify the data in this field
· +... Right justify the data in this field
The justification value may be followed by a number giving the minimum number of spaces to use for the data.
实例:
# cat ch21
# !/bin/bash
# \
exec tclsh "$0" "$@"
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"
执行结果:
# ./ch21
tolower converts this: THIS IS A STRING IN UPPER CASE LETTERS
to this: this is a string in upper case letters
toupper converts this: this is a string in lower case letters
to this: THIS IS A STRING IN LOWER CASE LETTERS
trimright converts this: This string has trailing dots ....
to this: This string has trailing dots
trimleft converts this: ....This string has leading dots
to this: This string has leading dots
trim converts this: ((this string is nested in parens )))
to this: this string is nested in parens
Example of format:
Item Cost
Tomatoes 30 Cents Each
Peppers 20 Cents Each
Onions 10 Cents Each
Steak 3.60 per Lb.