What is a string? edit
Within Tcl, the basic philosophy is that everything is a string. That is to say, Tcl's primary fundamental data structure is an item which is dynamically allocated (size limited only by the machine's memory and any limits put onto Tcl from the operating system or parent process) and which can contain any type of byte (at least as of Tcl 8.0 [is that version right?]).Other data structures (such as proc, dict, list, handle) are built on top of this fundamental assumption.The Tcl string command edit
string - Manipulate stringshttp://purl.org/tcl/home/man/tcl8.5/TclCmd/string.htmEnsembliform command for manipulating strings.string bytelength stringstring compare ?-nocase? ?-length int? string1 string2string equal ?-nocase? ?-length int? string1 string2string first string1 string2 ?startIndex?string index string charIndexstring is class ?-strict? ?-failindex varname? stringstring last string1 string2 ?startIndex?string length stringstring map ?-nocase? charMap stringstring match ?-nocase? pattern stringstring range string first laststring repeat string countstring replace string first last ?newstring?string reverse stringstring 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 charIndexstring wordstart string charIndex
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 1Sadly, 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 the [string is] command 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 functions for binary dataThe following subcommands check for the ByteArray object type internally based on their bytecode versions (as of 8.5.0):
- string range
- string index
- string match
- string length
- string compare (both objects must be ByteArrays)
- string first
- string last
- string map
- string replace
- string reverse
MG Since Tcl 8.5, an index in the string commands can include basic math;
string range $string $startChar+1 $endChar-1is 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:
% set string "This is a test string"
This is a test string
% set startChar 3
3
% set endChar 12
12
% 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 iterationUsing expr there is quite drastically faster...See also:
