Debian Codegraph @

doc/string.n

raw
1 .\"
2 .\" Copyright (c) 1993 The Regents of the University of California.
3 .\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 .\"
5 .\" See the file "license.terms" for information on usage and redistribution
6 .\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 .\"
8 .TH string n 8.1 Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 .\" Note: do not modify the .SH NAME line immediately below!
12 .SH NAME
13 string \- Manipulate strings
14 .SH SYNOPSIS
15 \fBstring \fIoption arg \fR?\fIarg ...\fR?
16 .BE
17 .SH DESCRIPTION
18 .PP
19 Performs one of several string operations, depending on \fIoption\fR.
20 The legal \fIoption\fRs (which may be abbreviated) are:
21 .\" METHOD: cat
22 .TP
23 \fBstring cat\fR ?\fIstring1\fR? ?\fIstring2...\fR?
24 .VS 8.6.2
25 Concatenate the given \fIstring\fRs just like placing them directly
26 next to each other and return the resulting compound string. If no
27 \fIstring\fRs are present, the result is an empty string.
28 .RS
29 .PP
30 This primitive is occasionally handier than juxtaposition of strings
31 when mixed quoting is wanted, or when the aim is to return the result
32 of a concatenation without resorting to \fBreturn\fR \fB\-level 0\fR,
33 and is more efficient than building a list of arguments and using
34 \fBjoin\fR with an empty join string.
35 .RE
36 .VE
37 .\" METHOD: compare
38 .TP
39 \fBstring compare\fR ?\fB\-nocase\fR? ?\fB\-length\fI length\fR? \fIstring1 string2\fR
40 .
41 Perform a character-by-character comparison of strings \fIstring1\fR
42 and \fIstring2\fR. Returns \-1, 0, or 1, depending on whether
43 \fIstring1\fR is lexicographically less than, equal to, or greater
44 than \fIstring2\fR. If \fB\-length\fR is specified, then only the
45 first \fIlength\fR characters are used in the comparison. If
46 \fB\-length\fR is negative, it is ignored. If \fB\-nocase\fR is
47 specified, then the strings are compared in a case-insensitive manner.
48 .\" METHOD: equal
49 .TP
50 \fBstring equal\fR ?\fB\-nocase\fR? ?\fB\-length\fI length\fR? \fIstring1 string2\fR
51 .
52 Perform a character-by-character comparison of strings \fIstring1\fR
53 and \fIstring2\fR. Returns 1 if \fIstring1\fR and \fIstring2\fR are
54 identical, or 0 when not. If \fB\-length\fR is specified, then only
55 the first \fIlength\fR characters are used in the comparison. If
56 \fB\-length\fR is negative, it is ignored. If \fB\-nocase\fR is
57 specified, then the strings are compared in a case-insensitive manner.
58 .\" METHOD: first
59 .TP
60 \fBstring first \fIneedleString haystackString\fR ?\fIstartIndex\fR?
61 .
62 Search \fIhaystackString\fR for a sequence of characters that exactly match
63 the characters in \fIneedleString\fR. If found, return the index of the
64 first character in the first such match within \fIhaystackString\fR. If not
65 found, return \-1. If \fIstartIndex\fR is specified (in any of the
66 forms described in \fBSTRING INDICES\fR), then the search is
67 constrained to start with the character in \fIhaystackString\fR specified by
68 the index. For example,
69 .RS
70 .PP
71 .CS
72 \fBstring first a 0a23456789abcdef 5\fR
73 .CE
74 .PP
75 will return \fB10\fR, but
76 .PP
77 .CS
78 \fBstring first a 0123456789abcdef 11\fR
79 .CE
80 .PP
81 will return \fB\-1\fR.
82 .RE
83 .\" METHOD: index
84 .TP
85 \fBstring index \fIstring charIndex\fR
86 .
87 Returns the \fIcharIndex\fR'th character of the \fIstring\fR argument.
88 A \fIcharIndex\fR of 0 corresponds to the first character of the
89 string. \fIcharIndex\fR may be specified as described in the
90 \fBSTRING INDICES\fR section.
91 .RS
92 .PP
93 If \fIcharIndex\fR is less than 0 or greater than or equal to the
94 length of the string then this command returns an empty string.
95 .RE
96 .TP
97 \fBstring is \fIclass\fR ?\fB\-strict\fR? ?\fB\-failindex \fIvarname\fR? \fIstring\fR
98 .
99 Returns 1 if \fIstring\fR is a valid member of the specified character
100 class, otherwise returns 0. If \fB\-strict\fR is specified, then an
101 empty string returns 0, otherwise an empty string will return 1 on
102 any class. If \fB\-failindex\fR is specified, then if the function
103 returns 0, the index in the string where the class was no longer valid
104 will be stored in the variable named \fIvarname\fR. The \fIvarname\fR
105 will not be set if \fBstring is\fR returns 1. The following character
106 classes are recognized (the class name can be abbreviated):
107 .RS
108 .IP \fBalnum\fR 12
109 Any Unicode alphabet or digit character.
110 .IP \fBalpha\fR 12
111 Any Unicode alphabet character.
112 .IP \fBascii\fR 12
113 Any character with a value less than \eu0080 (those that are in the
114 7\-bit ascii range).
115 .IP \fBboolean\fR 12
116 Any of the forms allowed to \fBTcl_GetBoolean\fR.
117 .IP \fBcontrol\fR 12
118 Any Unicode control character.
119 .IP \fBdigit\fR 12
120 Any Unicode digit character. Note that this includes characters
121 outside of the [0\-9] range.
122 .IP \fBdouble\fR 12
123 Any of the forms allowed to \fBTcl_GetDoubleFromObj\fR.
124 .IP \fBentier\fR 12
125 .VS 8.6
126 Any of the valid string formats for an integer value of arbitrary size
127 in Tcl, with optional surrounding whitespace. The formats accepted are
128 exactly those accepted by the C routine \fBTcl_GetBignumFromObj\fR.
129 .VE
130 .IP \fBfalse\fR 12
131 Any of the forms allowed to \fBTcl_GetBoolean\fR where the value is
132 false.
133 .IP \fBgraph\fR 12
134 Any Unicode printing character, except space.
135 .IP \fBinteger\fR 12
136 Any of the valid string formats for a 32-bit integer value in Tcl,
137 with optional surrounding whitespace. In case of overflow in
138 the value, 0 is returned and the \fIvarname\fR will contain \-1.
139 .IP \fBlist\fR 12
140 Any proper list structure, with optional surrounding whitespace. In
141 case of improper list structure, 0 is returned and the \fIvarname\fR
142 will contain the index of the
143 .QW element
144 where the list parsing fails, or \-1 if this cannot be determined.
145 .IP \fBlower\fR 12
146 Any Unicode lower case alphabet character.
147 .IP \fBprint\fR 12
148 Any Unicode printing character, including space.
149 .IP \fBpunct\fR 12
150 Any Unicode punctuation character.
151 .IP \fBspace\fR 12
152 Any Unicode whitespace character, mongolian vowel separator
153 (U+180e), zero width space (U+200b), word joiner (U+2060) or
154 zero width no-break space (U+feff) (=BOM).
155 .IP \fBtrue\fR 12
156 Any of the forms allowed to \fBTcl_GetBoolean\fR where the value is
157 true.
158 .IP \fBupper\fR 12
159 Any upper case alphabet character in the Unicode character set.
160 .IP \fBwideinteger\fR 12
161 Any of the valid forms for a wide integer in Tcl, with optional
162 surrounding whitespace. In case of overflow in the value, 0 is
163 returned and the \fIvarname\fR will contain \-1.
164 .IP \fBwordchar\fR 12
165 Any Unicode word character. That is any alphanumeric character, and
166 any Unicode connector punctuation characters (e.g. underscore).
167 .IP \fBxdigit\fR 12
168 Any hexadecimal digit character ([0\-9A\-Fa\-f]).
169 .PP
170 In the case of \fBboolean\fR, \fBtrue\fR and \fBfalse\fR, if the
171 function will return 0, then the \fIvarname\fR will always be set to
172 0, due to the varied nature of a valid boolean value.
173 .RE
174 .\" METHOD: last
175 .TP
176 \fBstring last \fIneedleString haystackString\fR ?\fIlastIndex\fR?
177 .
178 Search \fIhaystackString\fR for a sequence of characters that exactly match
179 the characters in \fIneedleString\fR. If found, return the index of the
180 first character in the last such match within \fIhaystackString\fR. If there
181 is no match, then return \-1. If \fIlastIndex\fR is specified (in any
182 of the forms described in \fBSTRING INDICES\fR), then only the
183 characters in \fIhaystackString\fR at or before the specified \fIlastIndex\fR
184 will be considered by the search. For example,
185 .RS
186 .PP
187 .CS
188 \fBstring last a 0a23456789abcdef 15\fR
189 .CE
190 .PP
191 will return \fB10\fR, but
192 .PP
193 .CS
194 \fBstring last a 0a23456789abcdef 9\fR
195 .CE
196 .PP
197 will return \fB1\fR.
198 .RE
199 .\" METHOD: length
200 .TP
201 \fBstring length \fIstring\fR
202 .
203 Returns a decimal string giving the number of characters in
204 \fIstring\fR. Note that this is not necessarily the same as the
205 number of bytes used to store the string. If the value is a
206 byte array value (such as those returned from reading a binary encoded
207 channel), then this will return the actual byte length of the value.
208 .\" METHOD: map
209 .TP
210 \fBstring map\fR ?\fB\-nocase\fR? \fImapping string\fR
211 .
212 Replaces substrings in \fIstring\fR based on the key-value pairs in
213 \fImapping\fR. \fImapping\fR is a list of \fIkey value key value ...\fR
214 as in the form returned by \fBarray get\fR. Each instance of a
215 key in the string will be replaced with its corresponding value. If
216 \fB\-nocase\fR is specified, then matching is done without regard to
217 case differences. Both \fIkey\fR and \fIvalue\fR may be multiple
218 characters. Replacement is done in an ordered manner, so the key
219 appearing first in the list will be checked first, and so on.
220 \fIstring\fR is only iterated over once, so earlier key replacements
221 will have no affect for later key matches. For example,
222 .RS
223 .PP
224 .CS
225 \fBstring map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc\fR
226 .CE
227 .PP
228 will return the string \fB01321221\fR.
229 .PP
230 Note that if an earlier \fIkey\fR is a prefix of a later one, it will
231 completely mask the later one. So if the previous example is
232 reordered like this,
233 .PP
234 .CS
235 \fBstring map {1 0 ab 2 a 3 abc 1} 1abcaababcabababc\fR
236 .CE
237 .PP
238 it will return the string \fB02c322c222c\fR.
239 .RE
240 .\" METHOD: match
241 .TP
242 \fBstring match\fR ?\fB\-nocase\fR? \fIpattern string\fR
243 .
244 See if \fIpattern\fR matches \fIstring\fR; return 1 if it does, 0 if
245 it does not. If \fB\-nocase\fR is specified, then the pattern attempts
246 to match against the string in a case insensitive manner. For the two
247 strings to match, their contents must be identical except that the
248 following special sequences may appear in \fIpattern\fR:
249 .RS
250 .IP \fB*\fR 10
251 Matches any sequence of characters in \fIstring\fR, including a null
252 string.
253 .IP \fB?\fR 10
254 Matches any single character in \fIstring\fR.
255 .IP \fB[\fIchars\fB]\fR 10
256 Matches any character in the set given by \fIchars\fR. If a sequence
257 of the form \fIx\fB\-\fIy\fR appears in \fIchars\fR, then any
258 character between \fIx\fR and \fIy\fR, inclusive, will match. When
259 used with \fB\-nocase\fR, the end points of the range are converted to
260 lower case first. Whereas {[A\-z]} matches
261 .QW _
262 when matching case-sensitively (since
263 .QW _
264 falls between the
265 .QW Z
266 and
267 .QW a ),
268 with \fB\-nocase\fR this is considered like {[A\-Za\-z]} (and
269 probably what was meant in the first place).
270 .IP \fB\e\fIx\fR 10
271 Matches the single character \fIx\fR. This provides a way of avoiding
272 the special interpretation of the characters \fB*?[]\e\fR in
273 \fIpattern\fR.
274 .RE
275 .\" METHOD: range
276 .TP
277 \fBstring range \fIstring first last\fR
278 .
279 Returns a range of consecutive characters from \fIstring\fR, starting
280 with the character whose index is \fIfirst\fR and ending with the
281 character whose index is \fIlast\fR (using the forms described in
282 \fBSTRING INDICES\fR). An index of \fB0\fR refers to the first
283 character of the string; an index of \fBend\fR refers to last
284 character of the string. \fIfirst\fR and \fIlast\fR may be specified
285 as for the \fBindex\fR method. If \fIfirst\fR is less than zero then
286 it is treated as if it were zero, and if \fIlast\fR is greater than or
287 equal to the length of the string then it is treated as if it were
288 \fBend\fR. If \fIfirst\fR is greater than \fIlast\fR then an empty
289 string is returned.
290 .\" METHOD: repeat
291 .TP
292 \fBstring repeat \fIstring count\fR
293 .
294 Returns a string consisting of \fIstring\fR concatenated with itself
295 \fIcount\fR times. If \fIcount\fR is 0, the empty string will be
296 returned.
297 .\" METHOD: replace
298 .TP
299 \fBstring replace \fIstring first last\fR ?\fInewstring\fR?
300 .
301 Removes a range of consecutive characters from \fIstring\fR, starting
302 with the character whose index is \fIfirst\fR and ending with the
303 character whose index is \fIlast\fR (using the forms described in
304 \fBSTRING INDICES\fR). An index of 0 refers to the
305 first character of the string. \fIFirst\fR and \fIlast\fR may be
306 specified as for the \fBindex\fR method. If \fInewstring\fR is
307 specified, then it is placed in the removed character range. If
308 \fIfirst\fR is less than zero then it is treated as if it were zero,
309 and if \fIlast\fR is greater than or equal to the length of the string
310 then it is treated as if it were \fBend\fR. The initial string is
311 returned untouched, if \fIfirst\fR is greater than \fIlast\fR, or if
312 \fIfirst\fR is equal to or greater than the length of the initial string,
313 or \fIlast\fR is less than 0.
314 .\" METHOD: reverse
315 .TP
316 \fBstring reverse \fIstring\fR
317 .
318 Returns a string that is the same length as \fIstring\fR but with its
319 characters in the reverse order.
320 .\" METHOD: tolower
321 .TP
322 \fBstring tolower \fIstring\fR ?\fIfirst\fR? ?\fIlast\fR?
323 .
324 Returns a value equal to \fIstring\fR except that all upper (or title)
325 case letters have been converted to lower case. If \fIfirst\fR is
326 specified, it refers to the first char index in the string to start
327 modifying. If \fIlast\fR is not specified, only the character at position
328 \fIfirst\fR is converted, otherwise \fIlast\fR refers to the char index
329 in the string to stop at (inclusive). \fIfirst\fR and \fIlast\fR may be
330 specified using the forms described in \fBSTRING INDICES\fR.
331 .\" METHOD: totitle
332 .TP
333 \fBstring totitle \fIstring\fR ?\fIfirst\fR? ?\fIlast\fR?
334 .
335 Returns a value equal to \fIstring\fR except that the first character
336 in \fIstring\fR is converted to its Unicode title case variant (or
337 upper case if there is no title case variant) and the rest of the
338 string is converted to lower case. If \fIfirst\fR is specified, it
339 refers to the first char index in the string to start modifying. If
340 \fIlast\fR is not specified, only the character at position \fIfirst\fR
341 is converted, otherwise \fIlast\fR refers to the char index in
342 the string to stop at (inclusive). \fIfirst\fR and \fIlast\fR may be
343 specified using the forms described in \fBSTRING INDICES\fR.
344 .\" METHOD: toupper
345 .TP
346 \fBstring toupper \fIstring\fR ?\fIfirst\fR? ?\fIlast\fR?
347 .
348 Returns a value equal to \fIstring\fR except that all lower (or title)
349 case letters have been converted to upper case. If \fIfirst\fR is
350 specified, it refers to the first char index in the string to start
351 modifying. If \fIlast\fR is not specified, only the character at position
352 \fIfirst\fR is converted, otherwise \fIlast\fR refers to the char index
353 in the string to stop at (inclusive). \fIfirst\fR and \fIlast\fR may be
354 specified using the forms described in \fBSTRING INDICES\fR.
355 .\" METHOD: trim
356 .TP
357 \fBstring trim \fIstring\fR ?\fIchars\fR?
358 .
359 Returns a value equal to \fIstring\fR except that any leading or
360 trailing characters present in the string given by \fIchars\fR are removed. If
361 \fIchars\fR is not specified then white space is removed (any character
362 for which \fBstring is space\fR returns 1, and "\e0").
363 .\" METHOD: trimleft
364 .TP
365 \fBstring trimleft \fIstring\fR ?\fIchars\fR?
366 .
367 Returns a value equal to \fIstring\fR except that any leading
368 characters present in the string given by \fIchars\fR are removed. If
369 \fIchars\fR is not specified then white space is removed (any character
370 for which \fBstring is space\fR returns 1, and "\e0").
371 .\" METHOD: trimright
372 .TP
373 \fBstring trimright \fIstring\fR ?\fIchars\fR?
374 .
375 Returns a value equal to \fIstring\fR except that any trailing
376 characters present in the string given by \fIchars\fR are removed. If
377 \fIchars\fR is not specified then white space is removed (any character
378 for which \fBstring is space\fR returns 1, and "\e0").
379 .SS "OBSOLETE SUBCOMMANDS"
380 .PP
381 These subcommands are currently supported, but are likely to go away in a
382 future release as their functionality is either virtually never used or highly
383 misleading.
384 .\" METHOD: bytelength
385 .TP
386 \fBstring bytelength \fIstring\fR
387 .
388 Returns a decimal string giving the number of bytes used to represent
389 \fIstring\fR in memory when encoded as Tcl's internal modified UTF\-8;
390 Tcl may use other encodings for \fIstring\fR as well, and does not
391 guarantee to only use a single encoding for a particular \fIstring\fR.
392 Because UTF\-8 uses a variable number of bytes to represent Unicode
393 characters, the byte length will not be the same as the character
394 length in general. The cases where a script cares about the byte
395 length are rare.
396 .RS
397 .PP
398 In almost all cases, you should use the
399 \fBstring length\fR operation (including determining the length of a
400 Tcl byte array value). Refer to the \fBTcl_NumUtfChars\fR manual
401 entry for more details on the UTF\-8 representation.
402 .PP
403 Formally, the \fBstring bytelength\fR operation returns the content of
404 the \fIlength\fR field of the \fBTcl_Obj\fR structure, after calling
405 \fBTcl_GetString\fR to ensure that the \fIbytes\fR field is populated.
406 This is highly unlikely to be useful to Tcl scripts, as Tcl's internal
407 encoding is not strict UTF\-8, but rather a modified CESU\-8 with a
408 denormalized NUL (identical to that used in a number of places by
409 Java's serialization mechanism) to enable basic processing with
410 non-Unicode-aware C functions. As this representation should only
411 ever be used by Tcl's implementation, the number of bytes used to
412 store the representation is of very low value (except to C extension
413 code, which has direct access for the purpose of memory management,
414 etc.)
415 .PP
416 \fICompatibility note:\fR it is likely that this subcommand will be
417 withdrawn in a future version of Tcl. It is better to use the
418 \fBencoding convertto\fR command to convert a string to a known
419 encoding and then apply \fBstring length\fR to that.
420 .PP
421 .CS
422 \fBstring length\fR [encoding convertto utf-8 $theString]
423 .CE
424 .RE
425 .\" METHOD: wordend
426 .TP
427 \fBstring wordend \fIstring charIndex\fR
428 .
429 Returns the index of the character just after the last one in the word
430 containing character \fIcharIndex\fR of \fIstring\fR. \fIcharIndex\fR
431 may be specified using the forms in \fBSTRING INDICES\fR. A word is
432 considered to be any contiguous range of alphanumeric (Unicode letters
433 or decimal digits) or underscore (Unicode connector punctuation)
434 characters, or any single character other than these.
435 .\" METHOD: wordstart
436 .TP
437 \fBstring wordstart \fIstring charIndex\fR
438 .
439 Returns the index of the first character in the word containing character
440 \fIcharIndex\fR of \fIstring\fR. \fIcharIndex\fR may be specified using the
441 forms in \fBSTRING INDICES\fR. A word is considered to be any contiguous
442 range of alphanumeric (Unicode letters or decimal digits) or underscore
443 (Unicode connector punctuation) characters, or any single character other than
444 these.
445 .SH "STRING INDICES"
446 .PP
447 When referring to indices into a string (e.g., for \fBstring index\fR
448 or \fBstring range\fR) the following formats are supported:
449 .IP \fIinteger\fR 10
450 For any index value that passes \fBstring is integer \-strict\fR,
451 the char specified at this integral index (e.g., \fB2\fR would refer to the
452 .QW c
453 in
454 .QW abcd ).
455 .IP \fBend\fR 10
456 The last char of the string (e.g., \fBend\fR would refer to the
457 .QW d
458 in
459 .QW abcd ).
460 .IP \fBend\-\fIN\fR 10
461 The last char of the string minus the specified integer offset \fIN\fR (e.g.,
462 .QW \fBend\-1\fR
463 would refer to the
464 .QW c
465 in
466 .QW abcd ).
467 .IP \fBend+\fIN\fR 10
468 The last char of the string plus the specified integer offset \fIN\fR (e.g.,
469 .QW \fBend+\-1\fR
470 would refer to the
471 .QW c
472 in
473 .QW abcd ).
474 .IP \fIM\fB+\fIN\fR 10
475 The char specified at the integral index that is the sum of
476 integer values \fIM\fR and \fIN\fR (e.g.,
477 .QW \fB1+1\fR
478 would refer to the
479 .QW c
480 in
481 .QW abcd ).
482 .IP \fIM\fB\-\fIN\fR 10
483 The char specified at the integral index that is the difference of
484 integer values \fIM\fR and \fIN\fR (e.g.,
485 .QW \fB2\-1\fR
486 would refer to the
487 .QW b
488 in
489 .QW abcd ).
490 .PP
491 In the specifications above, the integer value \fIM\fR contains no
492 trailing whitespace and the integer value \fIN\fR contains no
493 leading whitespace.
494 .SH EXAMPLE
495 .PP
496 Test if the string in the variable \fIstring\fR is a proper non-empty
497 prefix of the string \fBfoobar\fR.
498 .PP
499 .CS
500 set length [\fBstring length\fR $string]
501 if {$length == 0} {
502 set isPrefix 0
503 } else {
504 set isPrefix [\fBstring equal\fR -length $length $string "foobar"]
505 }
506 .CE
507 .SH "SEE ALSO"
508 expr(n), list(n)
509 .SH KEYWORDS
510 case conversion, compare, index, integer value, match, pattern, string,
511 word, equal, ctype, character, reverse
512 .\" Local Variables:
513 .\" mode: nroff
514 .\" End: