明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1615|回复: 8

[提问] 字符串中如何提取数字

[复制链接]
发表于 2021-2-22 22:54:25 | 显示全部楼层 |阅读模式
已获取字符串形式为如100~1000,需要计算平均值((100+1000)/2=550)
如何把100和1000提取出来?
因为位数不固定(例如1~5或1000~1500等)所以无法用substr函数。
求助高手指点。谢谢。
发表于 2021-2-22 23:04:00 | 显示全部楼层
(setq str "1000~1500")
(setq lst (read (strcat "(" (vl-string-subst " " "~" str) ")")))
(setq pjz (* 0.5 (apply '+ lst)))

"1000~1500"
(1000 1500)
1250.0
 楼主| 发表于 2021-2-22 23:25:56 | 显示全部楼层
非常感谢,能否再帮我加个判断条件,即如果str中不包含~则表明只是数字本身,不用求平均值。
因为选择中有变化数也有非变换数(非变化数就是一个数字例如1000或1500等)。
发表于 2021-2-24 07:44:19 | 显示全部楼层
(if (vl-string-search "~" str)
    (progn
        (setq lst (read (strcat "(" (vl-string-subst " " "~" str) ")")))
        (setq pjz (* 0.5 (apply '+ lst)))
     )
)
发表于 2021-2-24 08:10:54 | 显示全部楼层
和我一样的需求,看看这个帖子吧。
http://bbs.mjtd.com/thread-180398-1-1.html
 楼主| 发表于 2021-2-24 11:22:01 | 显示全部楼层
(if (and (distof txtn)
     (= (strlen txtn)
           (strlen (rtos (distof txtn) 2 0))
      )
)
使用了distof进行对比来判断。
发表于 2021-2-24 13:54:23 | 显示全部楼层
  1. ;; (abc"100~1000")
  2. (defun abc (str / n n1 a b)
  3.   (if (setq n (vl-string-search "~" str))
  4.     (*(apply '+ (mapcar 'distof(list (substr str 1 n) (substr str (+ n 2)))))0.5)
  5.     str
  6.   )
  7. )
发表于 2021-3-4 14:25:58 | 显示全部楼层
MT:200KN TA:2960KN,如何读取其中的数字,然后再两数相减除一个指定的数n  (2960-200)/n
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-5-16 21:02 , Processed in 0.182573 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表