当前位置: 首页 > news >正文

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();}/// <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.生成效果

 

http://www.lryc.cn/news/296326.html

相关文章:

  • opencv C++ dnn模块调用yolov5以及Intel RealSense D435深度相机联合使用进行目标检测
  • 2024牛客寒假算法基础集训营1(视频讲解全部题目)
  • 第三百一十三回
  • 倒计时61天
  • npm后Truffle找不到命令(ubantu20系统)
  • 嵌入式学习第三篇——51单片机
  • RabbitMQ详解
  • CGAL::2D Arrangements-4
  • 终端命令提示符:如何查看我们电脑端口是否被占用和处理方式
  • elasticsearch重置密码操作
  • 从零开始手写mmo游戏从框架到爆炸(零)—— 导航
  • 机器学习7-K-近邻算法(K-NN)
  • 相机图像质量研究(7)常见问题总结:光学结构对成像的影响--镜片固化
  • 猫头虎分享已解决Bug || Go Error: cannot convert int to string
  • 前端bug手册
  • Elasticsearch中Document Routing特性
  • 【Git版本控制 03】远程操作
  • 【Git】Windows下通过Docker安装GitLab
  • flutter 操作mysql
  • c++阶梯之类与对象(中)< 续集 >
  • GitLag所有操作-汇总
  • JSch - 配置SFTP服务器SSH免密登录
  • RISC-V指令格式
  • Linux 文件比较工具
  • 【GAMES101】Lecture 17 材质
  • 数模.matlab画图
  • [word] word表格表头怎么取消重复出现? #媒体#笔记#职场发展
  • vue项目开发vscode配置
  • BUUCTF-Real-[Tomcat]CVE-2017-12615
  • Qt应用软件【协议篇】http协议get、post示例