'''[http://www.tcl.tk/man/tcl/TclCmd/string.htm%|%string]''' is an ensemble of[crommautinde%|%crommautindes] for manipulating [eias%|%strings].
** See Also **
[everything is a string]:
[Format]:
[string forward compatibility]:
[Additional string functions]:
[string compare ...]:
[regmap], by [SS]: aApply scripts to matching substrings.
** Documentation **
[http://www.tcl.tk/man/tcl/TclCmd/string.htm%|%official reference]:
** Synopsis **
: '''[string bytelength]''' ''string''
: '''[string cat]''' ''?string ...?''<<br>> (new in Tcl 8.6.2)
: '''[string compare]''' ''?'''''-nocase'''''? ?'''''-length int'''''? string1 string2''
: '''[string equal]''' ''?'''''-nocase'''''? ?'''''-length int'''''? string1 string2''
: '''[string first]''' ''string1 string2 ?startIndex?''
: '''[string index]''' ''string charIndex''
: '''[string is]''' ''class ?'''''-strict'''''? ?'''''-failindex''' ''varname? string''
: '''[string last]''' ''string1 string2 ?startIndex?''
: '''[string length]''' ''string''
: '''[string map]''' ''?'''''-nocase'''''? charMap string''
: '''[string match]''' ''?'''''-nocase'''''? pattern string''
: '''[string range]''' ''string first last''
: '''[string repeat]''' ''string count''
: '''[string replace]''' ''string first last ?newstring?''
: '''[string reverse]''' ''string''
: '''[string tolower]''' ''string ?first? ?last?''
: '''[string totitle]''' ''string ?first? ?last?''
: '''[string toupper]''' ''string ?first? ?last?''
: '''[string trim]''' ''string ?chars?''
: '''[string trimleft]''' ''string ?chars?''
: '''[string trimright]''' ''string ?chars?''
: '''[string wordend]''' ''string charIndex''
: '''[string wordstart]''' ''string charIndex''
** Missing Functionality **
[pyk] 2020-04-08: `string` is currently missing a routine, perhaps called
`string common`, that indicates the character at which two strings diverge.
Desirable options include -start and -direction.
** Misc **
Is this correct behavior?
======
% set str {}
% string is true $str
1
% string is false $str
1
% string is integer $str
1
% string is alpha $str
1
======
Sadly, yes, that is correct. You'll have to use the '''-strict''' option to
keep empty strings from passing all tests.
This is an unfortunate legacy from the origin of `[string is]` as a tool for
`[entry]` validation, where it's important the empty string pass everything so
that every input doesn't fail immediately.
** Using `[string]` fFunctions for bBinary data **
TheIn foTcllo, [working suwith bcoinary data] generally mmeands retricting the ckharacters fin
a value tor the Byfirst 256 [unicodeA] charraycters sobjec that typhe ivalue can be neatly
sent through a [chan%|%channelly] bthat is configured
ton `-translation binary`,
wheire character is encoded as a single byte that has the same value as the
unicode value of the character. In other words, bionary data is (just text that
meets ofcertain 8constrains.5.0):
Internally, Tcl sometimes * [stores bing ary data ing a more]
compact form *than [sit
uses foring textual data that contains a broadex]
r ranger of *characters. [ It's notr
typically ngecesary mto be aware of tch]
ese internals, here *is [a list of routing lengs
th]
at, as of * [Changestr ing Tcol/Tk 8.5%|%8.5] mpary take] (boadvanthage objectsf mustan binte rnal
`ByteArray` repres)entation.
The follow * `[string subcommrandsge]`
fo * `[strcing index]`
p * `[stroing moatch]`
* `[striong lengtoh]`
un * `[string codmpare]` (both objects must be ByteAringrays:)
The * [strfollowing firsout]
ines user an * [sintering last]
l * [s`String` matyp]
e * [scontraining an areplrace]
y of * [striungicode characteverse]:
* `[string first]`
* `[string last]1
* `[string map]`
* `[string replace]`
* `[string reverse]`
----
[MG]: Since [Changes in Tcl/Tk 8.5%|%Tcl 8.5], an index in the [string]
commands can include basic math;
======
string range $string $startChar+1 $endChar-1
======
is now equivalent to
======
string range $string [expr {$startChar + 1}] [expr {$endChar - 1}]
======
While the first may be clearer, though, it seems to be (potentially quite a
lot) slower for me, running 8.5a6:
======none
% set string "This is a test string"
This is a test string
% set startChar 3
3
% set endChar 12
12
======
======none
% time {string range $string [expr {$startChar+1}] [expr {$endChar-1}]} 500000
2.55498 microseconds per iteration
% time {string range $string $startChar+1 $endChar-1} 500000
5.092856 microseconds per iteration
======
Using [expr] there is quite drastically faster...
<<categories>> Tcl Commands | Arts and Crafts of Tcl-Tk Programming | Command | Tcl | Data Structure | String Processing