首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

string 变换 int

2012-07-29 
string 转换 intsds#3435 --3435 --0sd34sd34 --.34null--0sdfhghj ---0[解决办法]JScript c

string 转换 int
"sds#3435 "   -->     3435
" "                                   -->     0
"   sd34sd34 "   --.>   34
null                             -->   0
"sdfhghj "         --->   0

[解决办法]

JScript code
function cInt(val){        if(val==null){            return 0;        }else{            val=val.replace(/(^\s*)|(\s*$)/g, "");            if(isNaN(parseInt(val)))            {                while(isNaN(parseInt(val))&&val.length>0){                    val=val.substring(1);                    if(!isNaN(parseInt(val))){                        break;                    }                }                return isNaN(parseInt(val))?0:parseInt(val);            }else{                return parseInt(val);            }        }    }    alert(cInt("sds#3435 "));    alert(cInt(" "));    alert(cInt("   sd34sd34 "));    alert(cInt(null));    alert(cInt("sdfhghj "));
[解决办法]
VBScript code
s = "你的字串"if isnull(s) then s = ""Set re = New RegExpWith re  .Pattern = "\D"  .Global = True  .IgnoreCase = TrueEnd Withs = trim(re.Replace(s, " ")) & " 0"ar = split(s)s2 = ar(0)if s2="" then   s2 = "0"end ifresponse.write cint(s2) 

热点排行