字符串中如何提取数字
已获取字符串形式为如100~1000,需要计算平均值((100+1000)/2=550)如何把100和1000提取出来?
因为位数不固定(例如1~5或1000~1500等)所以无法用substr函数。
求助高手指点。谢谢。
(setq str "1000~1500")
(setq lst (read (strcat "(" (vl-string-subst " " "~" str) ")")))
(setq pjz (* 0.5 (apply '+ lst)))
"1000~1500"
(1000 1500)
1250.0 非常感谢,能否再帮我加个判断条件,即如果str中不包含~则表明只是数字本身,不用求平均值。
因为选择中有变化数也有非变换数(非变化数就是一个数字例如1000或1500等)。 (if (vl-string-search "~" str)
(progn
(setq lst (read (strcat "(" (vl-string-subst " " "~" str) ")")))
(setq pjz (* 0.5 (apply '+ lst)))
)
) 和我一样的需求,看看这个帖子吧。
http://bbs.mjtd.com/thread-180398-1-1.html 高手
(if (and (distof txtn)
(= (strlen txtn)
(strlen (rtos (distof txtn) 2 0))
)
)
使用了distof进行对比来判断。 ;; (abc"100~1000")
(defun abc (str / n n1 a b)
(if (setq n (vl-string-search "~" str))
(*(apply '+ (mapcar 'distof(list (substr str 1 n) (substr str (+ n 2)))))0.5)
str
)
) MT:200KN TA:2960KN,如何读取其中的数字,然后再两数相减除一个指定的数n(2960-200)/n
页:
[1]