How to remove string and take integer value in the words

Hello, as you see I have a string like this:

"Hello 1125 test 993 / com 999"

now I need to convert that to 1125993999 and remove all string, space and special words, I just need integer, how can I do this? like php echo intval('42'); // 42


For what it’s worth, PHP’s intval doesn’t work like that:

$ php -r 'var_dump(intval("Hello 1125 test 993 / com 999"));'

So do you want to replicate intval or do you want to pick and combine all numbers from a string?

1 Like

A regex replacement is probably the easiest way to do this.

String.replace(input, ~r/[^\d]/, "")

for <<c <- "Hello 1125 test 993 / com 999">>, x in 48..57, do: x


Hello, Thank you, I said that I just needed number in any string, I need pic and combine numbers from a string like this: "he154ll$%^9o" to 1549

it seems that this sounds is good

You probably want something like

"Hello 1125 test 993 / com 999"
|> String.replace(~r/[^\d]/, "")
|> String.to_integer()

Genius! I keep forgetting how well Elixir comprehensions work. Thanks for the reminder.

1 Like