目录
一、使用的方法
1.正则表达式
2.Char.IsDigit 方法
二、源码
1.源代码
2.生成效果
一、使用的方法
1.正则表达式
使用正则表达式Regex类的IsMatch方法,可以有效地判断用户输入的信息是否为有符号整数。
用于判断字符串是否有符号整数的正则表达式可以是:^(\+?|-)[1-9][0-9]*$,其中,^(\+?|-)表示匹配零个至1个+或1个-,“*”限定符用于限定指定的字符至少出现0次。
2.Char.IsDigit 方法
先用ToCharArray()静态方法把输入的字符串转成字符数组,判断数组首元素是+、-、数字或非法字符,再遍历数组剩余元素并用Char.IsDigit()方法判断数组是否是纯数字。
下面分享源码:
二、源码
1.源代码
// 用正则表达式判断字符串是否正整数或负整数
// 用Char.IsDigit方法判断字符数组是否正整数或负整数
namespace _080
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private TextBox? textBox1;
private Button? button1;
private Label? label1;
private Button? button2;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(146, 17),
Name = "textBox1",
Size = new Size(100, 23),
TabIndex = 2
};
//
// button1
//
button1 = new Button
{
Location = new Point(171, 44),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 1,
Text = "验证1",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(35, 23),
Name = "label1",
Size = new Size(80, 17),
TabIndex = 0,
Text = "输入字符串:"
};
//
// button2
//
button2 = new Button
{
Location = new Point(171, 71),
Name = "button2",
Size = new Size(75, 23),
TabIndex = 3,
Text = "验证2",
UseVisualStyleBackColor = true
};
button2.Click += Button2_Click;
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(280, 100),
TabIndex = 0,
TabStop = false,
Text = "验证是否有符号整数"
};
groupBox1.Controls.Add(button2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(304, 123);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "正则表达式判断输入是否有符号整数";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
/// <summary>
/// 用正则表达式验证是否有符号整数
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
if (!(textBox1!.Text == ""))
{
if (!IsNumber(textBox1!.Text.Trim()))
{
MessageBox.Show("输入的字符不是整数", "验证1");
}
else
{
MessageBox.Show("输入的字符是有符号整数", "验证1");
}
}
else
{
MessageBox.Show("输入的字符串不得为空", "验证1");
return;
}
}
/// <summary>
/// 先用ToCharArray()方法把输入的字符串转成字符数组
/// 判断数组首元素+、-、数字、还是非法字符;
/// 再对字符数组遍历,
/// 用Char.IsDigit()方法判断数组中剩余元素是否纯数字
/// </summary>
private void Button2_Click(object? sender, EventArgs e)
{
if (!(textBox1!.Text == ""))
{
char[] charArr = textBox1!.Text.ToCharArray();
if (charArr[0] == '+' || Char.IsDigit(charArr[0]))
{
for (int i = 1; i < charArr.Length; i++)
{
if (!Char.IsDigit(charArr[i]))
{
MessageBox.Show("输入的字符串含有非法字符", "验证2");
return;
}
}
MessageBox.Show("输入的字符串是正整数", "验证2");
}
else if (charArr[0] == '-')
{
for (int i = 1; i < charArr.Length; i++)
{
if (!Char.IsDigit(charArr[i]))
{
MessageBox.Show("输入的字符串含有非法字符", "验证2");
return;
}
}
MessageBox.Show("输入的字符串是负整数", "验证2");
}
else
{
MessageBox.Show("字符串首字符是非法字符", "验证2");
return;
}
}
else
{
MessageBox.Show("输入的字符串不得为空", "验证2");
return;
}
}
/// <summary>
/// 验证输入是否有符号整数
/// </summary>
/// <param name="number">用户输入的字符串</param>
/// <returns>方法返回布尔值</returns>
public static bool IsNumber(string number)
{
return MyRegex().IsMatch(number);
}
[System.Text.RegularExpressions.GeneratedRegex(@"^(\+?|-)[1-9][0-9]*$")]
private static partial System.Text.RegularExpressions.Regex MyRegex();
}
}
2.生成效果