如何在 julia 中剥离字符串
How to strip a string in julia
当我尝试 strip
Julia 中的字符串时,为什么会出现错误?
strip("Clean Monkeys", "s")
# expected
> "Clean Monkey"
# but got
> MethodError: objects of type String are not callable
在 Julia 中,字符周围的单撇号和双撇号之间存在差异(与 Python 和 R 等语言不同)。所以 "s"
被视为字符串而 's'
被视为字符。
Strip 只删除字符而不是字符串。从文档:
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"
正如@Jonas 所说 strip
将 Char
作为第二个参数。如果你想实际删除后面的 SubString
你总是可以使用正则表达式,例如:
julia> replace("Hello worldHello", r"Hello$"=>"")
"Hello world"
请注意,$
是字符串结尾锚点,因此仅删除了结尾 Hello
。
当我尝试 strip
Julia 中的字符串时,为什么会出现错误?
strip("Clean Monkeys", "s")
# expected
> "Clean Monkey"
# but got
> MethodError: objects of type String are not callable
在 Julia 中,字符周围的单撇号和双撇号之间存在差异(与 Python 和 R 等语言不同)。所以 "s"
被视为字符串而 's'
被视为字符。
Strip 只删除字符而不是字符串。从文档:
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"
正如@Jonas 所说 strip
将 Char
作为第二个参数。如果你想实际删除后面的 SubString
你总是可以使用正则表达式,例如:
julia> replace("Hello worldHello", r"Hello$"=>"")
"Hello world"
请注意,$
是字符串结尾锚点,因此仅删除了结尾 Hello
。