文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>实战Tcl和TK程序设计 §4 Tcl中的字符串处理

实战Tcl和TK程序设计 §4 Tcl中的字符串处理

时间:2007-06-30  来源:oychw

§4    Tcl中的字符串处理

本章描述了有关字符产操作和简单模式匹配.涉及的命令有: string, append, format, scan, and binary.

       查看帮助的方法:

[root@Meil89 welch_examples]# tclsh

% string test

bad option "test": must be bytelength, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart

 

Table 4-1. The string command

string bytelength str

Returns the number of bytes used to store a string, which may be different from the character length returned by string length because of UTF-8 encoding. See page 220 of Chapter 15 about Unicode and UTF-8.

string compare ?-nocase? ?-length len? str1 str2

Compares strings lexicographically. Use -nocase for case insensitive comparison. Use -length to limit the comparison to the first len characters. Returns 0 if equal, -1 if str1 sorts before str2, else 1.

string equal ?-nocase? str1 str2

Compares strings and returns 1 if they are the same. Use -nocase for case insensitive comparison.

string first subString string ?startIndex?

Returns the index in string of the first occurrence of subString, or -1 if string is not found. startIndex may be specified to start in the middle of string.

string index string index

Returns the character at the specified index. An index counts from zero. Use end for the last character.

string is class ?-strict? ?-failindex varname? string

Returns 1 if string belongs to class. If -strict, then empty strings never match, otherwise they always match. If -failindex is specified, then varname is assigned the index of the character in string that prevented it from being a member of class. See Table 4-3 on page 54 for character class names.

string last subString string ?startIndex?

Returns the index in string of the last occurrence of subString, or -1 if subString is not found. startIndex may be specified to start in the middle of string.

string length string

Returns the number of characters in string.

string map ?-nocase? charMap string

Returns a new string created by mapping characters in string according to the input, output list in charMap. See page 55.

string match ?-nocase? pattern str

Returns 1 if str matches the pattern, else 0. Glob-style matching is used. See page 53.

string range str i j

Returns the range of characters in str from i to j.

string repeat str count

Returns str repeated count times.

string replace str first last ?newstr?

Returns a new string created by replacing characters first through last with newstr, or nothing.

string tolower string ?first? ?last?

Returns string in lower case. first and last determine the range of string on which to operate.

string totitle string ?first? ?last?

Capitalizes string by replacing its first character with the Unicode title case, or upper case, and the rest with lower case. first and last determine the range of string on which to operate.

string toupper string ?first? ?last?

Returns string in upper case. first and last determine the range of string on which to operate.

string trim string ?chars?

Trims the characters in chars from both ends of string. chars defaults to whitespace.

string trimleft string ?chars?

Trims the characters in chars from the beginning of string. chars defaults to whitespace.

string trimright string ?chars?

Trims the characters in chars from the end of string. chars defaults to whitespace.

string wordend str ix

Returns the index in str of the character after the word containing the character at index ix.

string wordstart str ix

Returns the index in str of the first character in the word containing the character at index ix.

 

常用的操作有equel, match,tolower,totitile,toupper,trim,trimright,trimleft.

 

% string range hello 2 end

llo

% string range hello 2 end-1

ll

%

 

% string match a* alpha

1

 

字符串分类:

% if {![string is integer -strict test]} {

    error "Invalid input. Please enter a number."

}

Invalid input. Please enter a number.

 

Table 4-3. Character class names

alnum

Any alphabet or digit character.

alpha

Any alphabet character.

ascii

Any character with a 7-bit character code (i.e., less than 128.)

boolean

A valid Tcl boolean value, such as 0, 1, true, false (in any case).

control

Character code less than 32, and not NULL.

digit

Any digit character.

double

A valid floating point number.

false

A valid Tcl boolean false value, such as 0 or false (in any case).

graph

Any printing characters, not including space characters.

integer

A valid integer.

lower

A string in all lower case.

print

A synonym for alnum.

punct

Any punctuation character.

space

Space, tab, newline, carriage return, vertical tab, backspace.

true

A valid Tcl boolean true value, such as 1 or true (in any case).

upper

A string all in upper case.

wordchar

Alphabet, digit, and the underscore.

xdigit

Valid hexadecimal digits.

 

字符串映射:

string map {f p d l} food

ð     pool

 

string map {f p d ll oo u} food
=> pull

 

 

Example 4-4 Mapping Microsoft World special characters to ASCII
proc Dos2Unix {filename} {
   set input [open $filename]
   set output [open $filename.new]
   fconfigure $output -translation lf
   puts $output [string map {
      \223   "
      \224   "
      \222   '
      \226   -
   } [read $input]]
   close $input
 
   close $output

append 命令

用于附加字符串

 

 

Format

Table 4-4. Format conversions

d

Signed integer.

u

Unsigned integer.

i

Signed integer. The argument may be in hex (0x) or octal (0) format.

o

Unsigned octal.

x or X

Unsigned hexadecimal. 'x' gives lowercase results.

c

Map from an integer to the ASCII character it represents.

s

A string.

f

Floating point number in the format a.b.

e or E

Floating point number in scientific notation, a.bE+-c.

g or G

Floating point number in either %f or %e format, whichever is shorter

 

Table 4-5. Format flags

-

Left justify the field.

+

Always include a sign, either + or -.

space

Precede a number with a space, unless the number has a leading sign. Useful for packing numbers close together.

0

Pad with zeros.

#

Leading 0 for octal. Leading 0x for hex. Always include a decimal point in floating

Scan

scan abcABC {%[a-z]} result
=> 1
set result
=> abc

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载