19

I am aware of String.strip/2, but I cannot figure out its usage for removing whitespaces.

String.strip "_some_", ?_   #=> "some"

If the string had whitespaces instead of _, how does one strip them? Also, it would be great if someone can explain what ? means here.

2

2 Answers 2

48

The question mark preceding a character returns its codepoint, it's mentioned in the Unicode and Code Points section of the getting started guide:

In Elixir you can use a ? in front of a character literal to reveal its code point:

iex> ?a
97
iex> ?ł
322
1
  • 4
    And to strip simple whitespaces one can simply do ?\s. We should probably add such examples to the documentation. :) Nov 20, 2014 at 9:04
6

There's String.strip/1 which strips all unicode whitespace. So you can just

iex(2)> String.strip("\t\ntest   ")
"test"
2
  • Awesome. Do you happen to know what ? means here? Any documentation reference would help.
    – Alex
    Nov 18, 2014 at 18:05
  • 5
    Looking at iex it seems to mean a codepoint, for example ?\s #=> 32 and you can even String.strip(" test ", 32). I actually assumed it means a one-char String - ?a being a shorthand for "a" which would be a direct port of the ruby syntax. Nov 18, 2014 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.