Why Can't I Strip This Character From A String?

你离开我真会死。 提交于 2019-12-24 06:37:51

问题


Elixir 1.0, Erlang 17.3 on Windows 7 x64.

I type this code:

l = "[9,0]"
s = String.strip(l,"[")

And I get this:

** (FunctionClauseError) no function clause matching in String.lstrip/2 (elixir) lib/string.ex:527: String.lstrip("[9,0]", "[") (elixir) lib/string.ex:564: String.strip/2

What am I missing?

I also tried s = String.strip(l,",") and same error. Also tried s = String.strip(l,'[') same error.

What am I missing?


回答1:


You want to pass a character to String.strip/2:

s = String.strip(l, ?[)

As Shashidhar points out in a comment, String.strip/2 has been dropped from Elixir documentation and may be removed completely in future. The suggested replacement is String.trim/2 which takes a String as its second argument:

s = String.trim(l, "[")



回答2:


You should use a single char as a second param, not a string.

iex(42)> l = "[9,0]"             
"[9,0]"
iex(43)> s = String.strip(l, ?[) 
"9,0]"
iex(44)> s = String.strip(l, ?])
"[9,0"

See more in String docs on this http://elixir-lang.org/docs/stable/elixir/String.html#strip/2



来源:https://stackoverflow.com/questions/26047715/why-cant-i-strip-this-character-from-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!