This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- Clojure
- Clojure
- C++
- C#
- D
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Lisp
- Lisp
- Lua
- PHP
- Pascal
- Pascal
- Perl
- Perl
- Python
- Ruby
- Ruby
- Rust
- Scala
auto p = "no_more_slashes///".stripRight!(a => a == '/');
writeln(p); // no_more_slashes
(defun remove-trailing-slash (string)
"Return a copy of STRING removing one trailing slash character, if any."
(let ((position (position #\/ string :from-end t)))
(if (eql position (1- (length string)))
(subseq string 0 position)
(copy-seq string))))
(setf p (remove-trailing-slash p))
We only remove a single slash. To remove all ending slashes one can use STRING-RIGHT-TRIM.
POSITION returns the index of the first matching element. The FROM-END keyword causes it to work in reverse, returning the index of the last matching element.
Though it isn't strictly necessary to make a new copy of strings which don't end in a slash, SUBSEQ already does copy its argument and it seems best to be consistent.
POSITION returns the index of the first matching element. The FROM-END keyword causes it to work in reverse, returning the index of the last matching element.
Though it isn't strictly necessary to make a new copy of strings which don't end in a slash, SUBSEQ already does copy its argument and it seems best to be consistent.
{ local $/='/'; chomp $p }
Set locally-scoped $/ (a.k.a. $INPUT_RECORD_SEPARATOR when you 'use English') to '/' so that chomp will remove a trailing '/' instead of the default "\n".