I was using the command ‘string trimright’ to trim my string but I found that this command trims more than required.
My expression is “dssss.dcsss” If I use string trim command to trim the last few characters “.dcsss”, it trims the entire string. How can I deal with this?
Command:
set a [string trimright "dcssss.dcsss" ".dcsss"]
puts $a
Intended output:
dcsss
Actual output
""
The
string trimrightcommand treats its (optional) last argument as a set of characters to remove (and so.dcsssis the same assdc.to it), just likestring trimandstring trimleftdo; indeed,string trimis just like using bothstring trimrightandstring trimleftin succession. This makes it unsuitable for what you are trying to do; to remove a suffix if it is present, you can use several techniques:If what you’re doing really is filename manipulation, like it looks like, do use the first of these options. The
filecommand has some really useful commands for working with filenames in a cross-platform manner in it.