如题所述
ãæ¹æ³ä¸ãï¼ä½¿ç¨ try{} catch{} è¯å¥ã
æ们å¯ä»¥å¨tryè¯å¥åä¸è¯å¾å°stringç±»åçå符串åé转æ¢ä¸ºintç±»åï¼å¦æ该å符串ä¸æ¯æ°åå符串åä¼æåºå¼å¸¸ï¼è¿æ¶å¨catchè¯å¥åä¸å°±è½æè·å¼å¸¸ãä¸æ¦åç°å¼å¸¸ï¼åä¸æ¯æ°åå符串ã
æ们å¯ä»¥ä»¥ä¸ä¸ç§æ¹å¼å°stringç±»å转æ¢ä¸ºintç±»åã
ï¼1ï¼ int.Parse(string);
ï¼2ï¼ Convert.ToInt16(string); //å½æ°åå符串çä½æ°å¤§äº4çæ¶å请使ç¨Convert.ToInt32()
ï¼3ï¼ Convert.ToInt32(string);
æ·»å ä¸ä¸ªææ¬æ¡TextBox1ï¼åä¸ä¸ªæé®Button1ï¼å½ç¹å»æé®æ¶ï¼å¤æææ¬æ¡ä¸çå 容æ¯å¦ä¸ºæ°åå符串ï¼æ¯çè¯åè¾åºè½¬æ¢åçæ°å¼ã
protected void Button1_Click(object sender, EventArgs e)
{
string message = TextBox1.Text.Trim();
int result;
if(isNumberic(message,out result))
{
string tt="<script>alert('å¹é æåï¼è½¬æ¢åçæ´æ°ä¸º"+result+"')</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "", tt);
}
else
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('å¹é 失败ï¼')</script>");
}
protected bool isNumberic(string message,out int result)
{
//å¤ææ¯å¦ä¸ºæ´æ°å符串
//æ¯çè¯åå°å ¶è½¬æ¢ä¸ºæ°å并å°å ¶è®¾ä¸ºoutç±»åçè¾åºå¼ãè¿åtrue, å¦å为false
result = -1; //result å®ä¹ä¸ºout ç¨æ¥è¾åºå¼
try
{
//å½æ°åå符串ç为æ¯å°äº4æ¶ï¼ä»¥ä¸ä¸ç§é½å¯ä»¥è½¬æ¢ï¼ä»»éä¸ç§
//å¦æä½æ°è¶ è¿4çè¯ï¼è¯·éç¨Convert.ToInt32() åint.Parse()
//result = int.Parse(message);
//result = Convert.ToInt16(message);
result = Convert.ToInt32(message);
return true;
}
catch
{
return false;
}
}
ä¸è¿°æ¹æ³å¯ä»¥å°intæ¹ä¸ºdoubleï¼å¦ double.Parse()ï¼è¿æ ·å°±å¯ä»¥å¤ææ¯å¦ä¸ºæµ®ç¹æ°å符串
ãæ¹æ³äºãï¼éè¿æ£å表达å¼æ¥å¤æã
ç¨æ£å表达å¼æ¥éªè¯å符串æ¯å¦ä¸ºæ°åå符串ãæ们è¦ç¨å°Regexç±»çisMatch()æ¹æ³ã该类å¨System.Text.RegularExpressions; æ¨å¯ä»¥éè¿using System.Text.RegularExpressions;å¯¼å ¥å½å空é´æ¥è®¿é®Regexç±»ãä¹å¯ä»¥ç´æ¥éè¿System.Text.RegularExpressions.Regex æ¥è®¿é®ã
protected bool isNumberic(string message,out int result)
{
System.Text.RegularExpressions.Regex rex=
new System.Text.RegularExpressions.Regex(@"^\d+$");
result = -1;
if (rex.IsMatch(message))
{
result = int.Parse(message);
return true;
}
else
return false;
}
éè¿æ£å表达å¼å¤ææ¯å¦å¹é ï¼ä¸ä» å¯ä»¥ç¨æ¥åç®åçå¤æå¹é ï¼è¿å¯ä»¥è¿è¡ç²¾ç¡®çå¹é ï¼å¦å¤ææ¯å¦æ¯å ä½çæ°åå符串ï¼Emailå¹é çãæ£å表达å¼æ¯ä¸ç§å¾å¥½çæ¹æ³ã
?
protected void Button1_Click(object sender, EventArgs e)
{
string message = TextBox1.Text.Trim();
isNumeric(message); //å¤æå符串æ¯å¦ä¸º5为æ´æ°å符串
}
protected void isNumeric(string message)
{
if (message != "" && Regex.IsMatch(message, @"^\d{5}$"))
{
//æå
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('å¹é éè¿ï¼ç¡®å®æ¯äºä½çæ´æ°å符串')</script>");
}
else
//失败
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('å¹é 失败ï¼')</script>");
}
æ们å¯ä»¥å¨tryè¯å¥åä¸è¯å¾å°stringç±»åçå符串åé转æ¢ä¸ºintç±»åï¼å¦æ该å符串ä¸æ¯æ°åå符串åä¼æåºå¼å¸¸ï¼è¿æ¶å¨catchè¯å¥åä¸å°±è½æè·å¼å¸¸ãä¸æ¦åç°å¼å¸¸ï¼åä¸æ¯æ°åå符串ã
æ们å¯ä»¥ä»¥ä¸ä¸ç§æ¹å¼å°stringç±»å转æ¢ä¸ºintç±»åã
ï¼1ï¼ int.Parse(string);
ï¼2ï¼ Convert.ToInt16(string); //å½æ°åå符串çä½æ°å¤§äº4çæ¶å请使ç¨Convert.ToInt32()
ï¼3ï¼ Convert.ToInt32(string);
æ·»å ä¸ä¸ªææ¬æ¡TextBox1ï¼åä¸ä¸ªæé®Button1ï¼å½ç¹å»æé®æ¶ï¼å¤æææ¬æ¡ä¸çå 容æ¯å¦ä¸ºæ°åå符串ï¼æ¯çè¯åè¾åºè½¬æ¢åçæ°å¼ã
protected void Button1_Click(object sender, EventArgs e)
{
string message = TextBox1.Text.Trim();
int result;
if(isNumberic(message,out result))
{
string tt="<script>alert('å¹é æåï¼è½¬æ¢åçæ´æ°ä¸º"+result+"')</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "", tt);
}
else
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('å¹é 失败ï¼')</script>");
}
protected bool isNumberic(string message,out int result)
{
//å¤ææ¯å¦ä¸ºæ´æ°å符串
//æ¯çè¯åå°å ¶è½¬æ¢ä¸ºæ°å并å°å ¶è®¾ä¸ºoutç±»åçè¾åºå¼ãè¿åtrue, å¦å为false
result = -1; //result å®ä¹ä¸ºout ç¨æ¥è¾åºå¼
try
{
//å½æ°åå符串ç为æ¯å°äº4æ¶ï¼ä»¥ä¸ä¸ç§é½å¯ä»¥è½¬æ¢ï¼ä»»éä¸ç§
//å¦æä½æ°è¶ è¿4çè¯ï¼è¯·éç¨Convert.ToInt32() åint.Parse()
//result = int.Parse(message);
//result = Convert.ToInt16(message);
result = Convert.ToInt32(message);
return true;
}
catch
{
return false;
}
}
ä¸è¿°æ¹æ³å¯ä»¥å°intæ¹ä¸ºdoubleï¼å¦ double.Parse()ï¼è¿æ ·å°±å¯ä»¥å¤ææ¯å¦ä¸ºæµ®ç¹æ°å符串
ãæ¹æ³äºãï¼éè¿æ£å表达å¼æ¥å¤æã
ç¨æ£å表达å¼æ¥éªè¯å符串æ¯å¦ä¸ºæ°åå符串ãæ们è¦ç¨å°Regexç±»çisMatch()æ¹æ³ã该类å¨System.Text.RegularExpressions; æ¨å¯ä»¥éè¿using System.Text.RegularExpressions;å¯¼å ¥å½å空é´æ¥è®¿é®Regexç±»ãä¹å¯ä»¥ç´æ¥éè¿System.Text.RegularExpressions.Regex æ¥è®¿é®ã
protected bool isNumberic(string message,out int result)
{
System.Text.RegularExpressions.Regex rex=
new System.Text.RegularExpressions.Regex(@"^\d+$");
result = -1;
if (rex.IsMatch(message))
{
result = int.Parse(message);
return true;
}
else
return false;
}
éè¿æ£å表达å¼å¤ææ¯å¦å¹é ï¼ä¸ä» å¯ä»¥ç¨æ¥åç®åçå¤æå¹é ï¼è¿å¯ä»¥è¿è¡ç²¾ç¡®çå¹é ï¼å¦å¤ææ¯å¦æ¯å ä½çæ°åå符串ï¼Emailå¹é çãæ£å表达å¼æ¯ä¸ç§å¾å¥½çæ¹æ³ã
?
protected void Button1_Click(object sender, EventArgs e)
{
string message = TextBox1.Text.Trim();
isNumeric(message); //å¤æå符串æ¯å¦ä¸º5为æ´æ°å符串
}
protected void isNumeric(string message)
{
if (message != "" && Regex.IsMatch(message, @"^\d{5}$"))
{
//æå
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('å¹é éè¿ï¼ç¡®å®æ¯äºä½çæ´æ°å符串')</script>");
}
else
//失败
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('å¹é 失败ï¼')</script>");
}
温馨提示:答案为网友推荐,仅供参考