在IDL中识别一个字符串是否可以转换为double数字的函数IsDoubleString

function IsDoubleString,dblstr ;用法IDL>Status=IsDoubleString(dblstr) ;Status=0或1,成功为1,否则为0 ;以下识别正确的话,肯定可以使用: ;IDL>DoubleValue=fix(dblstr,type=5)转换为double数字 ;测试版本6.X,不要再IDL 7.x中使用,测试时发现内置函数STREGEX有问题 ;-------------------------------------------------------------- Status=1;假设可以转换 ;先去掉首尾空格 str = STRTRIM(dblstr,2) ;--------------------------------------------- ;查找并去掉末尾的非法字符 pos = STREGEX(str, '([^0-9.eE+-]|[+-.][Ee]|[eE].)') if pos GT -1 then begin str=strmid(str,0,pos) endif ;--------------------------------------------- ;转换为ASCII inputstr = byte(str) ;获取字符个数 nn = n_elements(inputstr) ;--------------------------------------------- ;判断第一个字符的合法性 if nn GT 0 then begin FirstDoubleStr=byte('+-1234567890.') index=where(FirstDoubleStr eq inputstr[0],count) if count eq 0 then Status=0 endif else begin ;如果字符的长度小于1,也不是合法字符 Status=0 endelse ;--------------------------------------------- ;判断第二个字符的合法性 if nn GT 1 then begin SecondDoubleStr=byte('1234567890.Ee') index=where(SecondDoubleStr eq inputstr[1],count) if count eq 0 then Status=0 endif ;--------------------------------------------- ;查找字符串中数字的总数,不能小于1 num_total=0 NumberStr=byte('1234567890') for n=0L,nn-1 DO BEGIN count=0; index=where(NumberStr eq inputstr[n],count) num_total=count+num_total endfor if num_total LT 1 then Status=0 ;================================= ;后续处理 ;if Status eq 1 then begin ;print,fix(dblstr,type=5) ;endif ;================================= ;--------------------------------------------- return,Status End


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值