string - Manipulate strings http://purl.org/tcl/home/man/tcl8.4/TclCmd/string.htm Ensembliform command for manipulating strings. ---- '''[string bytelength]''' ''string'' ---- '''[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'' At times, people ask which is better for determining whether a string is ''empty'' (null): [string equal x x$str] [string equal "" $str] ![string compare "" $b] [string length $str] == 0 $str eq "" The string length or string equal will be a bit quicker, as they will look to see if the strings are of equal size first. ---- '''[string map]''' ''?'''''-nocase'''''? charMap string'' Here's an example from [jenglish] that [lvirden] thought was pretty neat: string map -nocase { "<" "<" ">" ">" "≤" "<=" "≥" ">=" } $whatever ---- [string match] ''?'''''-nocase'''''? pattern string'' ---- '''[string range]''' ''string first last'' ---- '''[string repeat]''' ''string count'' ---- '''[string replace]''' ''string first last ?newstring?'' ---- '''[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?'' This command can be used many ways. One possible use is to strip pathnames and extensions off of filenames. ''For stripping off extensions from filenames, use'' '''[[file rootname]]''' ''instead of [[string trimright]]!'' Supposing you have a directory of .log files and you want to return the filename with out its' extension, try: foreach lfn [glob -directory ./Logs -nocomplain -- *.log] { lappend fn [string trimright [string trimleft $lfn ./Logs/] .log] } [GG: This only works if you can be sure that the basename of your logfile doesn't end in any combination of l, o and g. for example: % set str "smeagol.log" % set newstr [string trimright $str ".log"] % set newstr smea I like stripping leading and trailing whitespace and field delimiters with 'string trim' commands, but not filename manipulation. ] The trimleft will remove the pathname from the beginning of the string and trimright will remove the extension. Remember that this command will not save to a variable, therefore you must set the same or another variable: % set foo ../returned../ ../returned../ % string trim $foo ../ returned % set foo ../returned../ % set foo [string trim $foo ../] returned % set foo returned ---- '''string wordend''' ''string charIndex'' '''string wordstart''' ''string charIndex'' ---- lappend fn [string trimright [string trimleft $lfn ./Logs/] .log] is not a good idea. It will strip l, o and g from the end of the file name, similarly the front. the string trim_x commands remove any character from the ''set'' given as the last argument. Ian Gay ---- See also: * [string forward compatibility] * [Additional string functions] * [string compare ...] ---- [Tcl syntax help] - [Arts and Crafts of Tcl-Tk Programming] - [Category Command] (of [Tcl]) - [Category Data Structure]