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

前端 未结 13 696
别跟我提以往
别跟我提以往 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 16:10

    Also using (re-seq) function can extend the return value to a string containing all the numbers existing in the input string in order:

    (defn convert-to-int [s] (->> (re-seq #"\d" s) (apply str) (Integer.)))

    (convert-to-int "10not123") => 10123

    (type *1) => java.lang.Integer

提交回复
热议问题