tushar
How can I split a string only at spaces with unicode characters in it?
I am a newbie in Elixir and am trying to split String only at spaces the string contains unicode charcters too
String.split("Freude schöner Götterfunken", ~r/[^[:alnum:]-]/u, trim: true)
like above , this gives me a UnicodeConversionError , is there a way to do that ?
the output I want is
["Freude","schöner","Götterfunken"]
It would be great if someone could suggest something.
Marked As Solved
tushar
Thanks a lot for all your help ,
My requirement was I have to split a string on spaces discarding all the symbols except a few special characters like a ‘-’ , after lot of trial and errors the below regex solves it ,
String.split(sentence, ~r/[^[:alnum:]-]/ui, trim: true)
This is a great forum and hope it will help someone in need ![]()
Thanks again for the help , I come from Java and have started on elixir , you can imagine the pain its a whole new world for me , will post questions if any .
Also Liked
kip
String.split("Freude schöner Götterfunken") would be preferred since it will use the Unicode word break algorithm which is much more flexible than breaking on an ASCII space character.
iex> String.split("Freude schöner Götterfunken")
["Freude", "schöner", "Götterfunken"]
From the docs:
Divides a string into substrings at each Unicode whitespace occurrence with leading and trailing whitespace ignored. Groups of whitespace are treated as a single occurrence. Divisions do not occur on non-breaking whitespace.
benwilson512
This works for me:
iex(1)> String.split("Freude schöner Götterfunken", ~r/[^[:alnum:]-]/u, trim: true)
["Freude", "schöner", "Götterfunken"]
Is your shell not in UTF8 perhaps?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








