9

Is it possible to convert Erlang binary UTF-8 string (like <<"HELLO">>) to lowercase without converting it to list and back?

1

2 Answers 2

12

If you know how to lowercase unicode character and key words here are "without converting it to list and back", then the answer could be:

<< <<(unicode_to_lower(C))/utf8>> || <<C/utf8>> <= <<"HELLO">> >>.
4
  • 2
    @Kay: Having a working implementation of unicode_to_lower/1 is implied by the answer. Commented May 25, 2011 at 6:49
  • I knew I am missing something really simple! Thanks! Commented May 25, 2011 at 11:24
  • Note: This will only work on a very small subset (the ASCII range)! For some "values" You have to peek at the following bytes (I think up to 6 bytes). en.wikipedia.org/wiki/UTF-8 Commented May 25, 2011 at 16:59
  • 3
    @bsmr It will work not just for ASCII 1> [ C || <<C/utf8>> <= unicode:characters_to_binary("ПРИВЕТ") ]. [1055,1056,1048,1042,1045,1058] Commented May 25, 2011 at 17:18
11

string:lowercase in Erlang 20 works with binaries:

1> string:lowercase(<<"HELLO">>).
<<"hello">>

Your Answer

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

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