C#验证字符串是正整数还是负整数,正则表达式vs用Char.IsDigit 方法遍历字符数组

目录

一、使用的方法

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.生成效果

 

 

 

 

最近更新

  1. TCP协议是安全的吗?

    2024-02-02 16:52:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-02 16:52:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-02 16:52:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-02 16:52:03       20 阅读

热门阅读

  1. 2024美赛C题思路/代码:网球中的动量

    2024-02-02 16:52:03       37 阅读
  2. OpenStack平台镜像优化

    2024-02-02 16:52:03       29 阅读
  3. Vue中的插槽Slot的使用说明

    2024-02-02 16:52:03       31 阅读
  4. 开源软件的未来

    2024-02-02 16:52:03       31 阅读
  5. Servlet基础之配置 Servlet 及其映射

    2024-02-02 16:52:03       33 阅读
  6. 9 排序

    2024-02-02 16:52:03       31 阅读
  7. 【leetcode100-081到090】【动态规划】一维五题合集1

    2024-02-02 16:52:03       31 阅读
  8. leetcode-用队列实现栈

    2024-02-02 16:52:03       37 阅读