stanza

How do I check if a string is a valid integer?


Let's say I have

val s = "123"
val r = "a123h"

How do I check if s can be resolved to a valid integer?


Solution

  • The signature of to-int in the package core is

    public lostanza defn to-int (s:ref<String>) -> ref<False|Int>
    
    

    So you need :

    defn is-int? (s: String) -> True|False :
      to-int(s) is Int
    

    N.B.: to-int accepts hexadecimal 0x..., octal 0o... and binary 0b... string representations in addition to decimal ones.