In Clojure how can I convert a String to a number?

前端 未结 13 690
别跟我提以往
别跟我提以往 2020-12-12 15:34

I have various strings, some like \"45\", some like \"45px\". How how I convert both of these to the number 45?

13条回答
  •  情深已故
    2020-12-12 15:46

    This will work on 10px or px10

    (defn parse-int [s]
       (Integer. (re-find  #"\d+" s )))
    

    it will parse the first continuous digit only so

    user=> (parse-int "10not123")
    10
    user=> (parse-int "abc10def11")
    10
    

提交回复
热议问题