Reputation: 1529
Why do I get an error when I try to strip
a string in Julia?
strip("Clean Monkeys", "s")
# expected
> "Clean Monkey"
# but got
> MethodError: objects of type String are not callable
Upvotes: 5
Views: 4742
Reputation: 1529
In Julia there is a difference between single and double apostrophes around characters (unlike languages like Python and R). So "s"
is seen as string and 's'
as a character.
Strip only removes char(s) not string(s). From the docs:
strip(str::AbstractString, chars) -> SubString
# This works
strip("Clean Monkeys", 's')
> "Clean Monkey"
# you can also provide lists of characters
strip("Clean Monkeys", ['e', 'y', 's'])
> "Clean Monk"
Upvotes: 11
Reputation: 42194
Like it was said by @Jonas strip
takes a Char
as the second argument. If you want to actually remove a SubString
at the back you can always use a regular expression such as:
julia> replace("Hello worldHello", r"Hello$"=>"")
"Hello world"
Note that $
is an end-of-string anchor and hence only the ending Hello
was removed.
Upvotes: 4