C#验证字符串的长度,用正则表达式 vs 字符数组长度或字符串的长度

目录

一、使用的方法

1.使用正则表达式 

2.通过计算字符串的长度验证

二、实例

1.源码

2.生成效果


一、使用的方法

1.使用正则表达式 

        使用正则表达式可以判断和限制用户输入的字符串长度。

        比如验证用户密码不得少于8为,匹配的正则表达式"^.{8,}$",其中.{8,}表示匹配除换行符外的8个或8个以上的字符。

2.通过计算字符串的长度验证

        通过字符串对象的Length属性可以有效地判断和限制用户输入的字符串长度。同理,把字符串转换成字符数组,然后计算该字符数组的长度同样可以实现此功能。

        好啦,翠花,上源码

二、实例

        本文作者用两种方法实现标题的设计目的:

        验证1:使用正则表达式;

        验证2:(1)通过计算字符串的长度来判断;

                     (2)先把字符串转换成字符数组,然后计算字符数组的长度判断;

1.源码

// 用正则表达式验证字符串长度≥8
// 用字符数组的长度或字符串的长度
namespace _089
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private TextBox? textBox1;
        private Button? button1;
        private Label? label1;
        private Button? button2;

        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            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";
            Text = "正则表达式验证字符串长度";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
        }
        /// 
        /// 用正则表达式验证字符串长度≥8
        /// 
        private void Button1_Click(object? sender, EventArgs e)
        {
            if (textBox1!.Text != "")
            {
                if (!Islength8(textBox1!.Text))
                {
                    MessageBox.Show("字符串长度<8", "验证1");
                }
                else
                {
                    MessageBox.Show("字符串长度≥8", "验证1");
                }
            }
            else
            {
                MessageBox.Show("字符串不能为空", "验证1");
            }
        }
        /// 
        /// 通过计算字符串的长度验证;
        /// 通过把字符串转成字符数组,然后计算字符数组的长度验证;
        /// 
        private void Button2_Click(object? sender, EventArgs e)
        {
            if (textBox1!.Text != "")
            {
                //检测字符串转化的字符数组的长度
                char[] charArr = textBox1!.Text.ToCharArray();
                if (charArr.Length >= 8)
                {
                    MessageBox.Show("字符串长度≥8", "验证2");
                }
                else
                {
                    MessageBox.Show("字符串长度<8", "验证2");
                }
                //检测字符串的长度
                //if (textBox1!.Text.Length >= 8)
                //{
                //    MessageBox.Show("字符串长度≥8", "验证2");
                //}
                //else
                //{
                //    MessageBox.Show("字符串长度<8", "验证2");
                //}
            }
            else
            {
                MessageBox.Show("输入的字符不能为空", "验证2");
            }
        }

        /// 
        /// 验证字符串长度是否≥8
        /// 
        /// 用户输入的字符串
        /// 方法返回布尔值
        public static bool Islength8(string str_Length)
        {
            return MyRegex().IsMatch(str_Length);
        }

        [System.Text.RegularExpressions.GeneratedRegex(@"^.{8,}$")]
        private static partial System.Text.RegularExpressions.Regex MyRegex();
    }
}

2.生成效果

 C#验证字符串的长度,用正则表达式 vs 字符数组长度或字符串的长度_第1张图片

C#验证字符串的长度,用正则表达式 vs 字符数组长度或字符串的长度_第2张图片

C#验证字符串的长度,用正则表达式 vs 字符数组长度或字符串的长度_第3张图片

C#验证字符串的长度,用正则表达式 vs 字符数组长度或字符串的长度_第4张图片

你可能感兴趣的:(正则表达式)