string trimright

string trimright string ?chars?

Returns a value equal to string except that any 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).

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.


   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