问题
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