Extract integer from string in erlang

I have this variable Code in erlang which has this value "T00059"

I want to extract this value 59 from Code

I am trying to extract with this code this value "00059"

  NewCode=string:substr(Code, 2, length(Code)),

      

Now I want to know how we can eliminate the first zero before the first integer is not null

means how can we extract "59"

for example, if I have this value "Z00887", I would have to finalize this value 887

+3


source to share


2 answers


You can simply do (output from an interactive session erl

):

1> Code = "Z00887",
1> {NewCode, _Rest} = string:to_integer(string:substr(Code, 2, length(Code))),
1> NewCode.
887

      



(My answer in test with loop in erlang goes into more detail on the same problem)

+8


source


This code skips leading zeros. If you want to keep them, change $1

to$0



extract_integer([]) -> [];
extract_integer([H|T]) when (H >= $1) and (H =< $9) -> [H] ++ T;
extract_integer([_H|T]) -> extract_integer(T).

      

0


source







All Articles