Home > Mobile >  Strip() works strange in Julia
Strip() works strange in Julia

Time:09-29

I noticed that strip() doesn't react on '[' symbol (really weird)

julia> strip("a b[2]", '[')
"a b[2]"

julia> strip("a b[2]", ']')
"a b[2"

Does anyone know whats going on?

CodePudding user response:

strip is a combination of lstrip and rstrip i.e. it strips characters from the left (beginning) and the right (end) of the string. It's intended for cases where you want to remove excess whitespace around something (which is the default when you just pass in a string), or otherwise remove unnecessary characters that are surrounding your actual text.
This is a common meaning of strip in many other languages, eg. Python, Ruby, etc.

To remove characters from anywhere in a string, use replace:

 replace(s::AbstractString, pat=>r, [pat2=>r2, ...]; [count::Integer])

  .... To remove instances of
  pat from string, set r to the empty String ("").

julia> replace("a b[2]", '[' => "")
"a b2]"

CodePudding user response:

strip(str::AbstractString, chars) does, according to the docs,

remove leading and trailing characters from str

In "a b[2]", '[' is neither leading nor trailing. ']' is, though.

  • Related