Mooodi
Get a char from a string by index
Given a string, how can I get access to its character by index? Enum.at(“my_string”, 2) doesn’t work.
Or rather, not char, but a substring of length 1, by index.
Marked As Solved
cpgo
Try String.at("my_string",2)
https://hexdocs.pm/elixir/String.html#at/2
6
Also Liked
kip
ex_cldr Core Team
Maybe just worth noting this is quite an expensive operation since each grapheme has to be extracted up to the index. If you are processing characters in a string you may find one of these other approaches useful:
-
String.next_grapheme/1is a good way to help recursing over a string’s graphemes -
String.graphemes/1will decompose the string into its graphemes (each a single character string) -
String,to_charlist/1will decompose the string into its code points (integer code point values) - Binary pattern matching may be good if you know the index you want is in a constant place, for example:
iex(37)> string = "012345"
"012345"
iex(38)> << a :: utf8, b :: utf8, c :: utf8, _rest :: binary >> = string
"012345"
iex(39)> a
48 # <- returns the code point at index 0 of the string
4
Eiji
In this case use String.slice/2, for example:
iex> String.slice("elixir", 2..3)
"ix"
2
Popular in Questions
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New







