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

相关推荐
黄尚圈圈26 分钟前
Vue 中引入 ECharts 的详细步骤与示例
前端·vue.js·echarts
浮华似水1 小时前
简洁之道 - React Hook Form
前端
正小安3 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
IT良4 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
yufei-coder4 小时前
掌握 C# 中的 LINQ(语言集成查询)
windows·vscode·c#·visual studio
vvvae12345 小时前
分布式数据库
数据库
_.Switch5 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
雪域迷影5 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
一路向前的月光5 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   5 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发