在 Clojure 中,如何将字符串转换为数字? [英] In Clojure how can I convert a String to a number?

查看:17
本文介绍了在 Clojure 中,如何将字符串转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种字符串,有些像45",有些像45px".我如何将这两个转换为数字 45?

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

推荐答案

This will work on 10px or px10

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

这篇关于在 Clojure 中,如何将字符串转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆