Version 2 of string trim

Updated 2004-05-25 11:54:47 by MG

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).


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:


Tcl syntax help - Category Command