In C # programming, sometimes we need to determine whether a string is a numeric string, and we can do it in the following two ways.
" Method One ": Use the try{} catch{} statement.
We can attempt to convert a string variable of type string to an int type in a try statement block, and if the string is not a numeric string, an exception is thrown, in which case the exception can be caught in the catch statement block. Once an exception is found, it is not a numeric string.
We can convert a string type to an int type in three ways.
(1) Int. Parse (string);
(2) convert.toint16 (string); When the number of digits in the string is greater than 4 , use Convert.ToInt32 ()
(3) Convert.ToInt32 (string);
Add a text box TextBox1, and a button Button1, when the button is clicked, determines whether the content in the text box is a numeric string, and then outputs the converted value.
?
1234567891011121314151617181920212223242526272829303132 |
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
;
}
}
|
The above method can change int to double, such as double. Parse () so that you can determine whether a floating-point string
" method Two ": Judging by regular Expressions .
Use regular expressions to verify that the string is a numeric string. We're going to use the IsMatch () method of the Regex class. This class is in System.Text.RegularExpressions; You can pass the using System.Text.RegularExpressions; Import namespaces to access the Regex class. It can also be accessed directly through System.Text.RegularExpressions.Regex .
?
12345678910111213 |
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
;
}
|
The regular expression to determine whether the match, not only can be used to make a simple judgment match, but also can be accurate matching, such as the judge whether it is a six-bit numeric string, email matching and so on. Regular expressions are a good way to do this.
?
12345678910111213141516 |
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>"
);
}
|
C # Determines if a string is a numeric string
C # Determines if a string is a numeric string