Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip() works strange in Julia

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?

like image 609
Vladyslav Avatar asked Nov 24 '25 17:11

Vladyslav


2 Answers

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]"
like image 92
Sundar R Avatar answered Nov 26 '25 18:11

Sundar R


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.

like image 28
phipsgabler Avatar answered Nov 26 '25 18:11

phipsgabler