Forum Discussion

neufpas_66702's avatar
neufpas_66702
Icon for Nimbostratus rankNimbostratus
May 25, 2011

strip trailing slashes

Given a string with an arbitrary number of slashes (HTTP::path for example), how do I remove the final slash if it has one? I'm testing with ends_with "/" but after that... I've got nothing. I'm sure it's possible, but I honestly can't figure it out. This should be easy but it doesn't appear to be given the tools provided.

 

 

Any help?

 

 

3 Replies

  • Hi neufpas,

     

     

    You can use [string range $uri 0 end-1] to do this:

     

     

    set uri "/test/with/training/slash/" if {$uri ends_with "/"}{ log local0. "\$uri: $uri, stripped: [string range $uri 0 end-1]" }

     

     

    $uri: /test/with/training/slash/, stripped: /test/with/training/slash

     

     

    Aaron
  • Thank you! I read through as much of the documentation as I could find and I never saw anything about being able to do "end-1". That should be documented somewhere.
  • It's not described in detail in the string range section of the string man page but it's in the string index portion:

     

     

     

    http://www.tcl.tk/man/tcl8.4/TclCmd/string.htmM12

     

     

    end-integer

     

    The last char of the string minus the specified integer offset (e.g. end-1 would refer to the "c" in "abcd").

     

     

     

    Aaron