C#查找字符串中的所有数字: 面向对象的自定义方法 vs 使用char.IsDigit()方法

目录

一、涉及到的方法

1.面向对象的自定义方法

2.面向对象编程技术定义

3.使用char.IsDigit()方法

二、实例

1.源码

2.生成效果


一、涉及到的方法

1.面向对象的自定义方法

查找字符串中的所有数字时,首先将所有数字存储到一个字符串数组中,然后循环遍历要在其中查找数字的字符串,如果与定义的字符串数组中的某一项相匹配,则记录该项,循环执行该操作,最后得到的结果就是字符串中的所有数字。

实例中的查找1主要用到了面向对象编程中的自定义方法。

自定义方法主要用来完成类或对象的行为,在面向对象编程语言中,类或对象是通过方法来与外界交互的。所以,方法是类与外界交互的基本方式。

2.面向对象编程技术定义

面向对象编程技术也叫OOP技术,它是计算机软件开发中的一种新方法、新思想。在过去的面向过程编程中,经常将所有的代码都包含在几个模块中,这样,当做某些修改时,常常是牵一发而动全身,不容易开发和维护。使用面向对象技术开发软件时,也可以将软件分成几个模块,每个模块都有特定的功能,但是,模块之间是相互独立,同时又是相互联系的,模块的代码可以重用,这样大大增加了代码的使用率,有利于软件的开发和维护。

3.使用char.IsDigit()方法

先把输入的字符串转换成字符数组,遍历该字符数组并使用使用char.IsDigit()方法判断其是否为数字,如果是则输出该数字。

关于char.IsDigit()方法的用法,详见本文作者的其他文章,C#的Char 结构的像IsLetterOrDigit(Char)等常见的方法-CSDN博客 https://wenchm.blog.csdn.net/article/details/135354939

二、实例

本实例使用两种方法实现在字符串中查找到所有的数字,然后输出数字。

查找1:使用面向对象自定义方法,把查找功能设计为一个专有的方法,事件中调用方法;

查找2:使用char.IsDigit()方法;

1.源码

cs 复制代码
// 判断字符串中的数字并显示
// 使用char.IsDigit()方法
namespace _115
{
    public partial class Form1 : Form
    {
        private Label? label1;
        private TextBox? textBox1;
        private Button? button1;
        private TextBox? textBox2;
        private Button? button2;
        private TextBox? textBox3;

        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(12, 9),
                Name = "label1",
                Size = new Size(80, 17),
                TabIndex = 0,
                Text = "输入字符串:"
            };
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(98, 9),
                Multiline = true,
                Name = "textBox1",
                Size = new Size(149, 61),
                TabIndex = 1
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(12, 26),
                Name = "button1",
                Size = new Size(49, 23),
                TabIndex = 2,
                Text = "查找1",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // textBox2
            //
            textBox2 = new TextBox
            {
                Location = new Point(12, 76),
                Multiline = true,
                Name = "textBox2",
                Size = new Size(235, 50),
                TabIndex = 3
            };
            // 
            // button2
            // 
            button2 = new Button
            {
                Location = new Point(12, 49),
                Name = "button2",
                Size = new Size(49, 23),
                TabIndex = 4,
                Text = "查找2",
                UseVisualStyleBackColor = true
            };
            button2.Click += Button2_Click;
            // 
            // textBox3
            // 
            textBox3 = new TextBox
            {
                Location = new Point(12, 132),
                Multiline = true,
                Name = "textBox3",
                Size = new Size(235, 50),
                TabIndex = 5
            };
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(259, 194);
            Controls.Add(textBox3);
            Controls.Add(button2);
            Controls.Add(textBox2);
            Controls.Add(button1);
            Controls.Add(textBox1);
            Controls.Add(label1);
            Name = "Form1";
            Text = "查找字符串中的数字";
        }
        /// <summary>
        /// 查找1
        /// 调用判断是否数字的方法,逐个字符地读取
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            textBox2!.Text = "查找1的数字:";
            string strs = textBox1!.Text;
            for (int i = 0; i < strs.Length; i++)
            {
                string str = strs.Substring(i, 1);
                bool bl = GetNumeric(str);//判断字符是否为数字
                if (bl)
                {
                    textBox2.Text += str + "、";
                }
            }
        }
        /// <summary>
        /// 查找2
        /// 使用char.IsDigit()方法
        /// </summary>
        private void Button2_Click(object? sender, EventArgs e)
        {
            textBox3!.Text = "查找2的数字:";
            char[] chars = textBox1!.Text.ToCharArray();
            foreach (char c in chars)
            {
                if (char.IsDigit(c)) 
                {
                    textBox3!.Text += c + "、";
                }
            }
        }

        /// <summary>
        /// 查找数字方法
        /// 将所有数字存储到一个字符串数组中
        /// 判断字符是否包含数组中指定的数字
        /// </summary>
        public static bool GetNumeric(string str)
        {
            bool bl = false;
            string[] ArrayInt = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
            foreach (string num in ArrayInt)
            {
                if (num == str)
                {
                    bl = true;
                    break;
                }
            }
            return bl;
        }
    }
}

2.生成效果

相关推荐
JNU freshman17 分钟前
windows 开发
windows·c#
小小小小宇30 分钟前
ESLint 插件笔记
前端
纪伊路上盛名在39 分钟前
jupyter内核崩溃
前端·数据库·jupyter·生物信息·基因组·k-mer
曼汐 .1 小时前
Python实战应用-Python操作MySQL数据库
数据库·mysql
bcxwz6691 小时前
mysql 创建大写字母的表名失败
数据库·mysql
数据知道1 小时前
【系统分析师】第5章-基础知识:数据库系统(核心总结)
数据库·数据库系统·系统分析师
Mr_Xuhhh1 小时前
数据库期末
数据库·oracle
vace cc1 小时前
sql列中数据通过逗号分割的集合,对其中的值进行全表查重
数据库·sql
Net蚂蚁代码2 小时前
Angular入门的环境准备步骤工作
前端·javascript·angular.js
虾条_花吹雪2 小时前
5、Spring AI(MCPServer+MCPClient+Ollama)开发环境搭建_第一篇
数据库·人工智能·学习·spring·ai