远洋山水小区风水:.net(c#)如何将string型转换成int型

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/02 20:57:09
用Request接收上一个网页传来的值时,我只能定义string型接收,怎样把string型转换成int型?

Conver.ToInt16(string)
Conver.ToInt32(string)

Int32.Parse(string)

给你一个方法,判断字符串是否由数字组成(能不使用异常处理最好不使用):
public static bool IsNumber(String strNumber)
{
Regex objNotNumberPattern = new Regex(\"[^0-9.-]\");
Regex objTwoDotPattern = new Regex(\"[0-9]*[.][0-9]*[.][0-9]*\");
Regex objTwoMinusPattern = new Regex(\"[0-9]*[-][0-9]*[-][0-9]*\");
String strValidRealPattern = \"^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$\";
String strValidIntegerPattern = \"^([-]|[0-9])[0-9]*$\";
Regex objNumberPattern = new Regex(\"(\" + strValidRealPattern + \")|(\" + strValidIntegerPattern + \")\");

return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}

Conver.ToInt16(string)

最好加上异常处理 或者 判断Request得到的值是否为NULL或者字符串“”

目前还没见过这种用法的,留个名,我也想知道答案