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

相关推荐
Patrick在香港9 小时前
Python正则表达式实战:解析和验证香港身份证、车牌、电话格式
开发语言·python·正则表达式
吴声子夜歌2 天前
正则指引——Ruby
开发语言·正则表达式·ruby
吴声子夜歌2 天前
正则指引——Golang
开发语言·golang·正则表达式
吴声子夜歌3 天前
正则指引——PHP
开发语言·正则表达式·php
吴声子夜歌5 天前
正则指引——断言
正则表达式
吴声子夜歌5 天前
正则指引——匹配原理
java·正则表达式
吴声子夜歌7 天前
正则表达式——环视
开发语言·正则表达式
吴声子夜歌7 天前
正则表达式——边界
正则表达式
营养充电站7 天前
C++23 新特性在 CLion 中的实战体验
macos·docker·jupyter·正则表达式