日期:2014-05-17  浏览次数:20932 次

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 RegExp
With re
  .Pattern = "\D"
  .Global = True
  .IgnoreCase = True
End With

s = trim(re.Replace(s, " ")) & " 0"
ar = split(s)
s2 = ar(0)
if s2="" then
   s2 = "0"
end if
response.write cint(s2)