可能重复:
如何将 String 转换为 Int?
如何将 queryString 值更改为 (int)
string str_id;
str_id = Request.QueryString["id"];
int id = (int)str_id;
可能重复:
如何将 String 转换为 Int?
如何将 queryString 值更改为 (int)
string str_id;
str_id = Request.QueryString["id"];
int id = (int)str_id;
使用Int32.TryParse 方法安全地获取int
值:
int id;
string str_id = Request.QueryString["id"];
if(int.TryParse(str_id,out id))
{
//id now contains your int value
}
else
{
//str_id contained something else, i.e. not int
}
换成这个
string str_id;
str_id = Request.QueryString["id"];
int id = Convert.ToInt32(str_id);
或者更简单、更有效的
string str_id;
str_id = Request.QueryString["id"];
int id = int.Parse(str_id);
int id = Convert.ToInt32(str_id, CultureInfo.InvariantCulture);
有几种方法可以做到这一点
string str_id = Request.QueryString["id"];
int id = 0;
//this prevent exception being thrown in case query string value is not a valid integer
Int32.TryParse(str_id, out id); //returns true if str_id is a valid integer and set the value of id to the value. False otherwise and id remains zero
其他
int id = Int32.Parse(str_id); //will throw exception if string is not valid integer
int id = Convert.ToInt32(str_id); //will throw exception if string is not valid integer