目录
  • 一、使用的方法
    • 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();
        }
        /// <summary>
        /// 用正则表达式验证字符串长度≥8
        /// </summary>
        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");
            }
        }
        /// <summary>
        /// 通过计算字符串的长度验证;
        /// 通过把字符串转成字符数组,然后计算字符数组的长度验证;
        /// </summary>
        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");
            }
        }
 
        /// <summary>
        /// 验证字符串长度是否≥8
        /// </summary>
        /// <param name="str_Length">用户输入的字符串</param>
        /// <returns>方法返回布尔值</returns>
        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#实现验证字符串的长度的方法详解的文章就介绍到这了,更多相关C#验证字符串长度内容请搜索本网站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本网站!

您可能感兴趣的文章:

  • c#固定长度的随机字符串例子
  • C#截取中英文混合指定长度字符串实例
  • C#区分中英文按照指定长度截取字符串的方法
  • c#实现获取字符串阵列中元素最长或最短的长度
  • 超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)