Version 13 of string trim

Updated 2015-05-17 14:11:24 by pooryorick
string trim string ?chars?

Returns a value equal to string except that any leading or trailing characters from the set given by chars are removed. If chars is not specified then white space is removed (spaces, tabs, newlines, and carriage returns).

See also:

  • TIP #318 : Extend Default Whitespace in 'string trim' Beyond ASCII

escargo 2 Jun 2008 - This also related to behavior discussed in comp.lang.tcl with the subject, "string trim not trimming special space characters" (starting on 4 March 2008).

The characters removed by [string trim string] (with no chars argument) are not all the ones for which [string is space char] returns 1.

I think the tip should also include Unicode nonbreaking spaces.

LV you could either update the TIP page with your concerns, or post a comment over on the TCT mailing list.


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

MG While you can use it for filenames, the file command is designed specifically for working with filenames and paths. A more general use is to remove leading/trailing white space from a string, something like...

 (Griffiths) 9 % set foo {
 >             this is a test string    }

             this is a test string    
 (Griffiths) 10 % string trim $foo
 this is a test string


See Also

string
string trimleft
string trimright
string map
replaces substrings in a string (felipel)
Trimming indentation
textutil
Provides regular expression variants of the string trim commands, as well as trimPrefix an trimEmptyHeading.