我在iex中尝试了以下代码:
iex(13)> String.valid?(<<128>>)
false
iex(14)> String.valid?(<<191>>)
false
为什么不是两个有效的字符串?
最佳答案 像Ramon Snir说的那样,你需要使用utf8.从入门指南:
A string is a UTF-8 encoded binary. In order to understand exactly what we mean by that, we need to understand the difference between bytes and code points.
…
When representing code points in bytes, we need to encode them somehow. Elixir chose the UTF-8 encoding as its main and default encoding. When we say a string is a UTF-8 encoded binary, we mean a string is a bunch of bytes organized in a way to represent certain code points, as specified by the UTF-8 encoding.
<<128 :: utf8>> |> String.valid? # => true
您可以在以下网址阅读更多信息:Binaries, strings and char lists