C#验证字符串是否纯字母:用正则表达式 vs 用Char.IsLetter方法加遍历

目录

一、使用的方法

1.使用正则表达式

2.使用Char.IsLetter方法

二、实例

[1. 源码](#1. 源码)

2.生成效果


一、使用的方法

1.使用正则表达式

使用正则表达式可以验证用户输入的字符串是否为字母。匹配的正则表达式可以是:^[A-Za-z]+、\^\[A-Za-z\]{1,}、^[A-Za-z]*$。

2.使用Char.IsLetter方法

同样地,使用Char结构的IsLetter方法也可以实现此验证功能。使用Char结构的IsLetter方法可以依次判断用户输入字符串中的每一个字符是否为字母,如果为字母则返回true,否则返回false。

好啦,现在来分享源码吧:

二、实例

本实例作者用两种方法实现设计目的:

验证1:用正则表达式;

验证2:用Char.IsLetter方法;留下一个思考题给读者,假如判断用的是if(IsLetter()){}结果会如何?读者不妨试一下,体验一下两者的不同,体验对了不用告诉我,体验不到差别,那就留言给我。

1. 源码

cs 复制代码
//用正则表达式判断字符串是否字母
//用Char.IsLetter方法判断字符串是否字母
namespace _087
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private Button? button2;
        private Button? button1;
        private TextBox? textBox1;
        private Label? label1;

        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }
        
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(20, 34),
                Name = "label1",
                Size = new Size(80, 17),
                TabIndex = 0,
                Text = "输入字符串:"
            };
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(106, 31),
                Name = "textBox1",
                Size = new Size(156, 23),
                TabIndex = 1
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(106, 57),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 2,
                Text = "验证1",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // button2
            // 
            button2 = new Button
            {
                Location = new Point(187, 57),
                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, 11),
                Name = "groupBox1",
                Size = new Size(280, 100),
                TabIndex = 0,
                TabStop = false,
                Text = "验证是否字母"
            };
            groupBox1.Controls.Add(button2);
            groupBox1.Controls.Add(button1);
            groupBox1.Controls.Add(textBox1);
            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>
        /// 用正则表达式验证
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            if(textBox1!.Text != "")
            {
                if (!IsLetter(textBox1!.Text.Trim()))
                {
                    MessageBox.Show("输入的不是纯字母", "验证1");
                }
                else
                {
                    MessageBox.Show("输入的是纯字母", "验证1");
                }
            }
            else
            {
                MessageBox.Show("字符串不能为空", "验证1");
            }
        }
        /// <summary>
        /// 用Char.IsLetter方法判断字符串是否字母
        /// </summary>
        private void Button2_Click(object? sender, EventArgs e)
        {
            if(textBox1!.Text != "")
            {
                foreach (char c in textBox1!.Text)
                {
                    if (!char.IsLetter(c))
                    {
                        MessageBox.Show("输入的不是纯字母", "验证2");
                        return;
                    }
                    else{ }
                }
                MessageBox.Show("输入的是纯字母", "验证2");
            }
            else
            {
                MessageBox.Show("字符串不能为空", "验证2");
            }
        }

        /// <summary>
        /// 验证字符串是否为大小写字母组成
        /// 等效的正则:^[A-Za-z]{1,}$、^[A-Za-z]+$
        /// </summary>
        /// <param name="str_Letter">字符串</param>
        /// <returns>方法返回布尔值</returns>
        public static bool IsLetter(string str_Letter)
        {
            return MyRegex().IsMatch(str_Letter);
        }

        [System.Text.RegularExpressions.GeneratedRegex(@"^[A-Za-z]*$")]
        private static partial System.Text.RegularExpressions.Regex MyRegex();
    }
}

2.生成效果

相关推荐
Bianca4275 小时前
Excel正则表达式.获取字符
正则表达式·excel
狮子座的男孩17 小时前
js基础:10、函数对象方法(call/apply)、arguments类数组对象、Date对象、Math工具类、包装类、字符串方法、正则表达式
前端·javascript·正则表达式·包装类·字符串方法·arguments·date对象
夏玉林的学习之路18 小时前
正则表达式
数据库·c++·qt·mysql·正则表达式
壹号用户3 天前
python学习之正则表达式
python·学习·正则表达式
xiaozaq4 天前
java 正则表达式 所有的优先级
java·开发语言·正则表达式
ColderYY4 天前
Python中的正则表达式
开发语言·python·正则表达式
李宥小哥4 天前
正则表达式详解
正则表达式
珊瑚礁的猪猪侠6 天前
正则表达式入门到精通教程(Linux实操版)
linux·人工智能·正则表达式
!win !8 天前
分享二个实用正则
javascript·正则表达式
xw58 天前
分享二个实用正则
javascript·正则表达式