当前位置: 代码迷 >> ASP >> string 变换 int
  详细解决方案

string 变换 int

热度:612   发布时间:2012-07-29 15:26:13.0
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) 
  相关解决方案