Skip to content
Tcl Source Code Artifact [5a241d4459]
Login

Artifact [5a241d4459]

Artifact 5a241d4459671e9e301b0fe5d7cb62931d6d64d890cc0e829369de0593daa672:



CommandName: string ManualSection: n Version: 8.1 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

string - Manipulate strings

Synopsis

::: {.synopsis} ::: [string]{.cmd} [option]{.arg} [arg]{.arg} [arg]{.optdot} :::

Description

Performs one of several string operations, depending on fIoptionfR. The legal fIoptionfRs (which may be abbreviated) are:

string cat ?fIstring1fR? ?fIstring2...fR? : Concatenate the given fIstringfRs just like placing them directly next to each other and return the resulting compound string. If no fIstringfRs are present, the result is an empty string. This primitive is occasionally handier than juxtaposition of strings when mixed quoting is wanted, or when the aim is to return the result of a concatenation without resorting to fBreturnfR fB-level 0fR, and is more efficient than building a list of arguments and using fBjoinfR with an empty join string.

string compare ?fB-nocasefR? ?fB-lengthfR fIlengthfR? fIstring1 string2fR : Perform a character-by-character comparison of strings fIstring1fR and fIstring2fR. Returns -1, 0, or 1, depending on whether fIstring1fR is lexicographically less than, equal to, or greater than fIstring2fR. If fB-lengthfR is specified, then only the first fIlengthfR characters are used in the comparison. If fB-lengthfR is negative, it is ignored. If fB-nocasefR is specified, then the strings are compared in a case-insensitive manner.

string equal ?fB-nocasefR? ?fB-lengthfR fIlengthfR? fIstring1 string2fR : Perform a character-by-character comparison of strings fIstring1fR and fIstring2fR. Returns 1 if fIstring1fR and fIstring2fR are identical, or 0 when not. If fB-lengthfR is specified, then only the first fIlengthfR characters are used in the comparison. If fB-lengthfR is negative, it is ignored. If fB-nocasefR is specified, then the strings are compared in a case-insensitive manner.

string first fIneedleString haystackStringfR ?fIstartIndexfR? : Search fIhaystackStringfR for a sequence of characters that exactly match the characters in fIneedleStringfR. If found, return the index of the first character in the first such match within fIhaystackStringfR. If not found, return -1. If fIstartIndexfR is specified (in any of the forms described in fBSTRING INDICESfR), then the search is constrained to start with the character in fIhaystackStringfR specified by the index. For example,

```
string first a 0a23456789abcdef 5
```
will return \fB10\fR, but

```
string first a 0123456789abcdef 11
```
will return \fB-1\fR.

string index fIstring charIndexfR : Returns the fIcharIndexfR'th character of the fIstringfR argument. A fIcharIndexfR of 0 corresponds to the first character of the string. fIcharIndexfR may be specified as described in the fBSTRING INDICESfR section. If fIcharIndexfR is less than 0 or greater than or equal to the length of the string then this command returns an empty string.

string insert fIstring index insertStringfR : Returns a copy of fIstringfR with fIinsertStringfR inserted at the fIindexfR'th character. The fIindexfR may be specified as described in the fBSTRING INDICESfR section. If fIindexfR is start-relative, the first character inserted in the returned string will be at the specified index. If fIindexfR is end-relative, the last character inserted in the returned string will be at the specified index. If fIindexfR is at or before the start of fIstringfR (e.g., fIindexfR is fB0fR), fIinsertStringfR is prepended to fIstringfR. If fIindexfR is at or after the end of fIstringfR (e.g., fIindexfR is fBendfR), fIinsertStringfR is appended to fIstringfR.

string is fIclassfR ?fB-strictfR? ?fB-failindexfR fIvarnamefR? fIstringfR : Returns 1 if fIstringfR is a valid member of the specified character class, otherwise returns 0. If fB-strictfR is specified, then an empty string returns 0, otherwise an empty string will return 1 on any class. If fB-failindexfR is specified, then if the function returns 0, the index in the string where the class was no longer valid will be stored in the variable named fIvarnamefR. The fIvarnamefR will not be set if fBstring isfR returns 1. The following character classes are recognized (the class name can be abbreviated):

alnum : Any Unicode alphabet or digit character.

alpha : Any Unicode alphabet character.

ascii : Any character with a value less than u0080 (those that are in the 7-bit ascii range).

boolean : Any of the forms allowed to fBTcl_GetBooleanfR.

control : Any Unicode control character.

dict : Any proper dict structure, with optional surrounding whitespace. In case of improper dict structure, 0 is returned and the fIvarnamefR will contain the index of the "element" where the dict parsing fails, or -1 if this cannot be determined.

digit : Any Unicode digit character. Note that this includes characters outside of the [0-9] range.

double : Any of the forms allowed to fBTcl_GetDoubleFromObjfR.

entier : Synonym for fBintegerfR.

false : Any of the forms allowed to fBTcl_GetBooleanfR where the value is false.

graph : Any Unicode printing character, except space.

integer : Any of the valid string formats for an integer value of arbitrary size in Tcl, with optional surrounding whitespace. The formats accepted are exactly those accepted by the C routine fBTcl_GetBignumFromObjfR.

list : Any proper list structure, with optional surrounding whitespace. In case of improper list structure, 0 is returned and the fIvarnamefR will contain the index of the "element" where the list parsing fails, or -1 if this cannot be determined.

lower : Any Unicode lower case alphabet character.

print : Any Unicode printing character, including space.

punct : Any Unicode punctuation character.

space : Any Unicode whitespace character, mongolian vowel separator (U+180e), zero width space (U+200b), word joiner (U+2060) or zero width no-break space (U+feff) (=BOM).

true : Any of the forms allowed to fBTcl_GetBooleanfR where the value is true.

upper : Any upper case alphabet character in the Unicode character set.

wideinteger : Any of the valid forms for a wide integer in Tcl, with optional surrounding whitespace. In case of overflow in the value, 0 is returned and the fIvarnamefR will contain -1.

wordchar : Any Unicode word character. That is any alphanumeric character, and any Unicode connector punctuation characters (e.g. underscore).

xdigit : Any hexadecimal digit character ([0-9A-Fa-f]).

In the case of \fBboolean\fR, \fBtrue\fR and \fBfalse\fR, if the function will return 0, then the \fIvarname\fR will always be set to 0, due to the varied nature of a valid boolean value.

string last fIneedleString haystackStringfR ?fIlastIndexfR? : Search fIhaystackStringfR for a sequence of characters that exactly match the characters in fIneedleStringfR. If found, return the index of the first character in the last such match within fIhaystackStringfR. If there is no match, then return -1. If fIlastIndexfR is specified (in any of the forms described in fBSTRING INDICESfR), then only the characters in fIhaystackStringfR at or before the specified fIlastIndexfR will be considered by the search. For example,

```
string last a 0a23456789abcdef 15
```
will return \fB10\fR, but

```
string last a 0a23456789abcdef 9
```
will return \fB1\fR.

string length fIstringfR : Returns a decimal string giving the number of characters in fIstringfR. Note that this is not necessarily the same as the number of bytes used to store the string. If the value is a byte array value (such as those returned from reading a binary encoded channel), then this will return the actual byte length of the value.

string map ?fB-nocasefR? fImapping stringfR : Replaces substrings in fIstringfR based on the key-value pairs in fImappingfR. fImappingfR is a list of fIkey value key value ...fR as in the form returned by fBarray getfR. Each instance of a key in the string will be replaced with its corresponding value. If fB-nocasefR is specified, then matching is done without regard to case differences. Both fIkeyfR and fIvaluefR may be multiple characters. Replacement is done in an ordered manner, so the key appearing first in the list will be checked first, and so on. fIstringfR is only iterated over once, so earlier key replacements will have no affect for later key matches. For example,

```
string map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc
```
will return the string \fB01321221\fR.
Note that if an earlier \fIkey\fR is a prefix of a later one, it will completely mask the later one.  So if the previous example is reordered like this,

```
string map {1 0 ab 2 a 3 abc 1} 1abcaababcabababc
```
it will return the string \fB02c322c222c\fR.

string match ?fB-nocasefR? fIpattern stringfR : See if fIpatternfR matches fIstringfR; return 1 if it does, 0 if it does not. If fB-nocasefR is specified, then the pattern attempts to match against the string in a case insensitive manner. For the two strings to match, their contents must be identical except that the following special sequences may appear in fIpatternfR:


: Matches any sequence of characters in fIstringfR, including a null string.

? : Matches any single character in fIstringfR.

[chars**]** : Matches any character in the set given by fIcharsfR. If a sequence of the form fIxfRfB-fRfIyfR appears in fIcharsfR, then any character between fIxfR and fIyfR, inclusive, will match. When used with fB-nocasefR, the end points of the range are converted to lower case first. Whereas {[A-z]} matches "" when matching case-sensitively (since "" falls between the "Z" and "a"), with fB-nocasefR this is considered like {[A-Za-z]} (and probably what was meant in the first place).

****x : Matches the single character fIxfR. This provides a way of avoiding the special interpretation of the characters fB*?[]\fR in fIpatternfR.

string range fIstring first lastfR : Returns a range of consecutive characters from fIstringfR, starting with the character whose index is fIfirstfR and ending with the character whose index is fIlastfR (using the forms described in fBSTRING INDICESfR). An index of fB0fR refers to the first character of the string; an index of fBendfR refers to last character of the string. fIfirstfR and fIlastfR may be specified as for the fBindexfR method. If fIfirstfR is less than zero then it is treated as if it were zero, and if fIlastfR is greater than or equal to the length of the string then it is treated as if it were fBendfR. If fIfirstfR is greater than fIlastfR then an empty string is returned.

string repeat fIstring countfR : Returns a string consisting of fIstringfR concatenated with itself fIcountfR times. If fIcountfR is 0, the empty string will be returned.

string replace fIstring first lastfR ?fInewstringfR? : Removes a range of consecutive characters from fIstringfR, starting with the character whose index is fIfirstfR and ending with the character whose index is fIlastfR (using the forms described in fBSTRING INDICESfR). An index of 0 refers to the first character of the string. fIFirstfR and fIlastfR may be specified as for the fBindexfR method. If fInewstringfR is specified, then it is placed in the removed character range. If fIfirstfR is less than zero then it is treated as if it were zero, and if fIlastfR is greater than or equal to the length of the string then it is treated as if it were fBendfR. The initial string is returned untouched, if fIfirstfR is greater than fIlastfR, or if fIfirstfR is equal to or greater than the length of the initial string, or fIlastfR is less than 0.

string reverse fIstringfR : Returns a string that is the same length as fIstringfR but with its characters in the reverse order.

string tolower fIstringfR ?fIfirstfR? ?fIlastfR? : Returns a value equal to fIstringfR except that all upper (or title) case letters have been converted to lower case. If fIfirstfR is specified, it refers to the first char index in the string to start modifying. If fIlastfR is specified, it refers to the char index in the string to stop at (inclusive). fIfirstfR and fIlastfR may be specified using the forms described in fBSTRING INDICESfR.

string totitle fIstringfR ?fIfirstfR? ?fIlastfR? : Returns a value equal to fIstringfR except that the first character in fIstringfR is converted to its Unicode title case variant (or upper case if there is no title case variant) and the rest of the string is converted to lower case. If fIfirstfR is specified, it refers to the first char index in the string to start modifying. If fIlastfR is specified, it refers to the char index in the string to stop at (inclusive). fIfirstfR and fIlastfR may be specified using the forms described in fBSTRING INDICESfR.

string toupper fIstringfR ?fIfirstfR? ?fIlastfR? : Returns a value equal to fIstringfR except that all lower (or title) case letters have been converted to upper case. If fIfirstfR is specified, it refers to the first char index in the string to start modifying. If fIlastfR is specified, it refers to the char index in the string to stop at (inclusive). fIfirstfR and fIlastfR may be specified using the forms described in fBSTRING INDICESfR.

string trim fIstringfR ?fIcharsfR? : Returns a value equal to fIstringfR except that any leading or trailing characters present in the string given by fIcharsfR are removed. If fIcharsfR is not specified then white space is removed (any character for which fBstring is spacefR returns 1, and "0").

string trimleft fIstringfR ?fIcharsfR? : Returns a value equal to fIstringfR except that any leading characters present in the string given by fIcharsfR are removed. If fIcharsfR is not specified then white space is removed (any character for which fBstring is spacefR returns 1, and "0").

string trimright fIstringfR ?fIcharsfR? : Returns a value equal to fIstringfR except that any trailing characters present in the string given by fIcharsfR are removed. If fIcharsfR is not specified then white space is removed (any character for which fBstring is spacefR returns 1, and "0").

Obsolete subcommands

These subcommands are currently supported, but are likely to go away in a future release as their functionality is either virtually never used or highly misleading.

string wordend fIstring charIndexfR : Returns the index of the character just after the last one in the word containing character fIcharIndexfR of fIstringfR. fIcharIndexfR may be specified using the forms in fBSTRING INDICESfR. A word is considered to be any contiguous range of alphanumeric (Unicode letters or decimal digits) or underscore (Unicode connector punctuation) characters, or any single character other than these.

string wordstart fIstring charIndexfR : Returns the index of the first character in the word containing character fIcharIndexfR of fIstringfR. fIcharIndexfR may be specified using the forms in fBSTRING INDICESfR. A word is considered to be any contiguous range of alphanumeric (Unicode letters or decimal digits) or underscore (Unicode connector punctuation) characters, or any single character other than these.

String indices

When referring to indices into a string (e.g., for fBstring indexfR or fBstring rangefR) the following formats are supported:

integer : For any index value that passes fBstring is integer -strictfR, the char specified at this integral index (e.g., fB2fR would refer to the "c" in "abcd").

end : The last char of the string (e.g., fBendfR would refer to the "d" in "abcd").

end-N : The last char of the string minus the specified integer offset fINfR (e.g., "end-1" would refer to the "c" in "abcd").

end+N : The last char of the string plus the specified integer offset fINfR (e.g., "end+-1" would refer to the "c" in "abcd").

M**+N : The char specified at the integral index that is the sum of integer values fIMfR and fINfR (e.g., "**1+1" would refer to the "c" in "abcd").

M**-N : The char specified at the integral index that is the difference of integer values fIMfR and fINfR (e.g., "**2-1" would refer to the "b" in "abcd").

In the specifications above, the integer value fIMfR contains no trailing whitespace and the integer value fINfR contains no leading whitespace.

Example

Test if the string in the variable fIstringfR is a proper non-empty prefix of the string fBfoobarfR.

set length [string length $string]
if {$length == 0} {
    set isPrefix 0
} else {
    set isPrefix [string equal -length $length $string "foobar"]
}