mgjohns61585
Convert a string to an integer?
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from a string to an int. Any help would be greatly appreciated.
Most Liked
tyro
Hi there!
To convert a string to an integer you can use String.to_integer/1 e.g.
int = String.to_integer("1")
When it comes to user input you might also want to remove whitespace first which you can do with String.trim/1.
Hope that helps!
benwilson512
Edited for code blocks. I recommend formatting code that you post, it helps others understand what’s going on to help you better.
In your case you wrote :string.to_integer but @tyro wrote String.to_integer. Elixir module names capitalized, erlang modules tend to be lower case. That’s why :rand.uniform uses lower case :rand, it’s an erlang module. String however is an Elixir module, you can find the documentation on it here: String — Elixir v1.20.2
Last Post!
mgjohns61585
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









