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

相关推荐
htj101 天前
C# 使用正则表达式
正则表达式·c#
ZZZKKKRTSAE1 天前
快速上手Linux全局搜索正则表达式(grep)
linux·服务器·正则表达式
Kusunoki_D2 天前
Python-正则表达式(re 模块)
python·正则表达式
数字芯片实验室2 天前
正则表达式的前世今生
正则表达式
Lenyiin3 天前
《 C++ 点滴漫谈: 四十 》文本的艺术:C++ 正则表达式的高效应用之道
c++·正则表达式·lenyiin
AA-代码批发V哥3 天前
Java正则表达式完全指南
java·正则表达式
coding随想4 天前
JavaScript中的正则表达式:文本处理的瑞士军刀
javascript·mysql·正则表达式
OldField-Tian4 天前
Qt中使用正则表达式来提取字符串
qt·正则表达式
datascome4 天前
简数采集技巧之快速获取特殊链接网址URL方法
前端·经验分享·爬虫·程序人生·正则表达式
WebCsDn_TDCode4 天前
正则表达式检测文件类型是否为视频或图片
javascript·正则表达式·音视频