📄 string.n
字号:
'\"'\" Copyright (c) 1993 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" RCS: @(#) $Id: string.n,v 1.17 2002/07/05 07:23:45 hobbs Exp $'\" .so man.macros.TH string n 8.1 Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEstring \- Manipulate strings.SH SYNOPSIS\fBstring \fIoption arg \fR?\fIarg ...?\fR.BE.SH DESCRIPTION.PPPerforms one of several string operations, depending on \fIoption\fR.The legal \fIoption\fRs (which may be abbreviated) are:.TP\fBstring bytelength \fIstring\fRReturns a decimal string giving the number of bytes used to represent\fIstring\fR in memory. Because UTF\-8 uses one to three bytes torepresent Unicode characters, the byte length will not be the same asthe character length in general. The cases where a script cares aboutthe byte length are rare. In almost all cases, you should use the\fBstring length\fR operation (including determining the length of aTcl ByteArray object). Refer to the \fBTcl_NumUtfChars\fR manualentry for more details on the UTF\-8 representation..TP\fBstring compare\fR ?\fB\-nocase\fR? ?\fB\-length int\fR? \fIstring1 string2\fRPerform a character-by-character comparison of strings \fIstring1\fRand \fIstring2\fR. Returns \-1, 0, or 1, depending on whether\fIstring1\fR is lexicographically less than, equal to, or greaterthan \fIstring2\fR. If \fB\-length\fR is specified, then only thefirst \fIlength\fR characters are used in the comparison. If\fB\-length\fR is negative, it is ignored. If \fB\-nocase\fR isspecified, then the strings are compared in a case-insensitive manner..TP\fBstring equal\fR ?\fB\-nocase\fR? ?\fB-length int\fR? \fIstring1 string2\fRPerform a character-by-character comparison of strings \fIstring1\fRand \fIstring2\fR. Returns 1 if \fIstring1\fR and \fIstring2\fR areidentical, or 0 when not. If \fB\-length\fR is specified, then onlythe first \fIlength\fR characters are used in the comparison. If\fB\-length\fR is negative, it is ignored. If \fB\-nocase\fR isspecified, then the strings are compared in a case-insensitive manner..TP\fBstring first \fIstring1 string2\fR ?\fIstartIndex\fR?Search \fIstring2\fR for a sequence of characters that exactly matchthe characters in \fIstring1\fR. If found, return the index of thefirst character in the first such match within \fIstring2\fR. If notfound, return \-1. If \fIstartIndex\fR is specified (in any of theforms accepted by the \fBindex\fR method), then the search isconstrained to start with the character in \fIstring2\fR specified bythe index. For example,.RS.CS\fBstring first a 0a23456789abcdef 5\fR.CEwill return \fB10\fR, but.CS\fBstring first a 0123456789abcdef 11\fR.CEwill return \fB\-1\fR..RE.TP\fBstring index \fIstring charIndex\fRReturns the \fIcharIndex\fR'th character of the \fIstring\fR argument.A \fIcharIndex\fR of 0 corresponds to the first character of thestring. \fIcharIndex\fR may be specified as follows:.RS.IP \fIinteger\fR 10The char specified at this integral index..IP \fBend\fR 10The last char of the string..IP \fBend\-\fIinteger\fR 10The last char of the string minus the specified integer offset(e.g. \fBend\-1\fR would refer to the "c" in "abcd")..PPIf \fIcharIndex\fR is less than 0 or greater than or equal to thelength of the string then an empty string is returned..RE.TP\fBstring is \fIclass\fR ?\fB\-strict\fR? ?\fB\-failindex \fIvarname\fR? \fIstring\fRReturns 1 if \fIstring\fR is a valid member of the specified characterclass, otherwise returns 0. If \fB\-strict\fR is specified, then anempty string returns 0, otherwise and empty string will return 1 onany class. If \fB\-failindex\fR is specified, then if the functionreturns 0, the index in the string where the class was no longer validwill be stored in the variable named \fIvarname\fR. The \fIvarname\fRwill not be set if the function returns 1. The following characterclasses are recognized (the class name can be abbreviated):.RS.IP \fBalnum\fR 10Any Unicode alphabet or digit character..IP \fBalpha\fR 10Any Unicode alphabet character..IP \fBascii\fR 10Any character with a value less than \\u0080 (those that are in the7\-bit ascii range)..IP \fBboolean\fR 10Any of the forms allowed to \fBTcl_GetBoolean\fR..IP \fBcontrol\fR 10Any Unicode control character..IP \fBdigit\fR 10Any Unicode digit character. Note that this includes charactersoutside of the [0\-9] range..IP \fBdouble\fR 10Any of the valid forms for a double in Tcl, with optional surroundingwhitespace. In case of under/overflow in the value, 0 is returned andthe \fIvarname\fR will contain \-1..IP \fBfalse\fR 10Any of the forms allowed to \fBTcl_GetBoolean\fR where the value isfalse..IP \fBgraph\fR 10Any Unicode printing character, except space..IP \fBinteger\fR 10Any of the valid forms for an integer in Tcl, with optionalsurrounding whitespace. In case of under/overflow in the value, 0 isreturned and the \fIvarname\fR will contain \-1..IP \fBlower\fR 10Any Unicode lower case alphabet character..IP \fBprint\fR 10Any Unicode printing character, including space..IP \fBpunct\fR 10Any Unicode punctuation character..IP \fBspace\fR 10Any Unicode space character..IP \fBtrue\fR 10Any of the forms allowed to \fBTcl_GetBoolean\fR where the value istrue..IP \fBupper\fR 10Any upper case alphabet character in the Unicode character set..IP \fBwordchar\fR 10Any Unicode word character. That is any alphanumeric character, andany Unicode connector punctuation characters (e.g. underscore)..IP \fBxdigit\fR 10Any hexadecimal digit character ([0\-9A\-Fa\-f])..PPIn the case of \fBboolean\fR, \fBtrue\fR and \fBfalse\fR, if thefunction will return 0, then the \fIvarname\fR will always be set to0, due to the varied nature of a valid boolean value..RE.TP\fBstring last \fIstring1 string2\fR ?\fIlastIndex\fR?Search \fIstring2\fR for a sequence of characters that exactly matchthe characters in \fIstring1\fR. If found, return the index of thefirst character in the last such match within \fIstring2\fR. If thereis no match, then return \-1. If \fIlastIndex\fR is specified (in anyof the forms accepted by the \fBindex\fR method), then only thecharacters in \fIstring2\fR at or before the specified \fIlastIndex\fRwill be considered by the search. For example,.RS.CS\fBstring last a 0a23456789abcdef 15\fR.CEwill return \fB10\fR, but.CS\fBstring last a 0a23456789abcdef 9\fR.CEwill return \fB1\fR..RE.TP\fBstring length \fIstring\fRReturns a decimal string giving the number of characters in\fIstring\fR. Note that this is not necessarily the same as thenumber of bytes used to store the string. If the object is aByteArray object (such as those returned from reading a binary encodedchannel), then this will return the actual byte length of the object..TP\fBstring map\fR ?\fB\-nocase\fR? \fIcharMap string\fRReplaces characters in \fIstring\fR based on the key-value pairs in\fIcharMap\fR. \fIcharMap\fR is a list of \fIkey value key value ...\fRas in the form returned by \fBarray get\fR. Each instance of akey in the string will be replaced with its corresponding value. If\fB\-nocase\fR is specified, then matching is done without regard tocase differences. Both \fIkey\fR and \fIvalue\fR may be multiplecharacters. Replacement is done in an ordered manner, so the keyappearing first in the list will be checked first, and so on.\fIstring\fR is only iterated over once, so earlier key replacementswill have no affect for later key matches. For example,.RS.CS\fBstring map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc\fR.CEwill return the string \fB01321221\fR..RE.TP\fBstring match\fR ?\fB\-nocase\fR? \fIpattern\fR \fIstring\fRSee if \fIpattern\fR matches \fIstring\fR; return 1 if it does, 0 ifit doesn't. If \fB\-nocase\fR is specified, then the pattern attemptsto match against the string in a case insensitive manner. For the twostrings to match, their contents must be identical except that thefollowing special sequences may appear in \fIpattern\fR:.RS.IP \fB*\fR 10Matches any sequence of characters in \fIstring\fR, including a nullstring..IP \fB?\fR 10Matches any single character in \fIstring\fR..IP \fB[\fIchars\fB]\fR 10Matches any character in the set given by \fIchars\fR. If a sequenceof the form \fIx\fB\-\fIy\fR appears in \fIchars\fR, then anycharacter between \fIx\fR and \fIy\fR, inclusive, will match. Whenused with \fB\-nocase\fR, the end points of the range are converted tolower case first. Whereas {[A\-z]} matches '_' when matchingcase-sensitively ('_' falls between the 'Z' and 'a'), with\fB\-nocase\fR this is considered like {[A\-Za\-z]} (and probably whatwas meant in the first place)..IP \fB\e\fIx\fR 10Matches the single character \fIx\fR. This provides a way of avoidingthe special interpretation of the characters \fB*?[]\e\fR in\fIpattern\fR..RE.TP\fBstring range \fIstring first last\fRReturns a range of consecutive characters from \fIstring\fR, startingwith the character whose index is \fIfirst\fR and ending with thecharacter whose index is \fIlast\fR. An index of 0 refers to the firstcharacter of the string. \fIfirst\fR and \fIlast\fR may be specifiedas for the \fBindex\fR method. If \fIfirst\fR is less than zero thenit is treated as if it were zero, and if \fIlast\fR is greater than orequal to the length of the string then it is treated as if it were\fBend\fR. If \fIfirst\fR is greater than \fIlast\fR then an emptystring is returned..TP\fBstring repeat \fIstring count\fRReturns \fIstring\fR repeated \fIcount\fR number of times..TP\fBstring replace \fIstring first last\fR ?\fInewstring\fR?Removes a range of consecutive characters from \fIstring\fR, startingwith the character whose index is \fIfirst\fR and ending with thecharacter whose index is \fIlast\fR. An index of 0 refers to thefirst character of the string. \fIFirst\fR and \fIlast\fR may bespecified as for the \fBindex\fR method. If \fInewstring\fR isspecified, then it is placed in the removed character range. If\fIfirst\fR is less than zero then it is treated as if it were zero,and if \fIlast\fR is greater than or equal to the length of the stringthen it is treated as if it were \fBend\fR. If \fIfirst\fR is greaterthan \fIlast\fR or the length of the initial string, or \fIlast\fR isless than 0, then the initial string is returned untouched..TP\fBstring tolower \fIstring\fR ?\fIfirst\fR? ?\fIlast\fR?Returns a value equal to \fIstring\fR except that all upper (or title)case letters have been converted to lower case. If \fIfirst\fR isspecified, it refers to the first char index in the string to startmodifying. If \fIlast\fR is specified, it refers to the char index inthe string to stop at (inclusive). \fIfirst\fR and \fIlast\fR may bespecified as for the \fBindex\fR method..TP\fBstring totitle \fIstring\fR ?\fIfirst\fR? ?\fIlast\fR?Returns a value equal to \fIstring\fR except that the first characterin \fIstring\fR is converted to its Unicode title case variant (orupper case if there is no title case variant) and the rest of thestring is converted to lower case. If \fIfirst\fR is specified, itrefers to the first char index in the string to start modifying. If\fIlast\fR is specified, it refers to the char index in the string tostop at (inclusive). \fIfirst\fR and \fIlast\fR may be specified asfor the \fBindex\fR method..TP\fBstring toupper \fIstring\fR ?\fIfirst\fR? ?\fIlast\fR?Returns a value equal to \fIstring\fR except that all lower (or title)case letters have been converted to upper case. If \fIfirst\fR isspecified, it refers to the first char index in the string to startmodifying. If \fIlast\fR is specified, it refers to the char index inthe string to stop at (inclusive). \fIfirst\fR and \fIlast\fR may bespecified as for the \fBindex\fR method..TP\fBstring trim \fIstring\fR ?\fIchars\fR?Returns a value equal to \fIstring\fR except that any leading ortrailing characters from the set given by \fIchars\fR are removed. If\fIchars\fR is not specified then white space is removed (spaces,tabs, newlines, and carriage returns)..TP\fBstring trimleft \fIstring\fR ?\fIchars\fR?Returns a value equal to \fIstring\fR except that any leadingcharacters from the set given by \fIchars\fR are removed. If\fIchars\fR is not specified then white space is removed (spaces,tabs, newlines, and carriage returns)..TP\fBstring trimright \fIstring\fR ?\fIchars\fR?Returns a value equal to \fIstring\fR except that any trailingcharacters from the set given by \fIchars\fR are removed. If\fIchars\fR is not specified then white space is removed (spaces,tabs, newlines, and carriage returns)..TP\fBstring wordend \fIstring charIndex\fRReturns the index of the character just after the last one in the wordcontaining character \fIcharIndex\fR of \fIstring\fR. \fIcharIndex\fRmay be specified as for the \fBindex\fR method. A word isconsidered to be any contiguous range of alphanumeric (Unicode lettersor decimal digits) or underscore (Unicode connector punctuation)characters, or any single character other than these..TP\fBstring wordstart \fIstring charIndex\fRReturns the index of the first character in the word containingcharacter \fIcharIndex\fR of \fIstring\fR. \fIcharIndex\fR may bespecified as for the \fBindex\fR method. A word is considered to be anycontiguous range of alphanumeric (Unicode letters or decimal digits)or underscore (Unicode connector punctuation) characters, or anysingle character other than these..SH "SEE ALSO"expr(n), list(n).SH KEYWORDScase conversion, compare, index, match, pattern, string, word, equal, ctype
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -