现在的位置: 首页 > 综合 > 正文

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

2013年02月16日 ⁄ 综合 ⁄ 共 1378字 ⁄ 字号 评论关闭
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

 

抱歉!评论已关闭.