程序员开发实例大全宝库

网站首页 > 编程文章 正文

源代码:C#按指定条件在数组中查找信息

zazugpt 2024-09-07 01:51:07 编程文章 22 ℃ 0 评论

C#按指定条件在数组中查找信息

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string[] vbook = { "活着", "动物农场 ", "瓦尔登湖", "当你像鸟飞往你的山", "看不见的大猩猩", "百年孤独", "人间词话", "影响力", "围城", "追寻生命的意义", "人性的弱点" };
        public Form1()
        {
            InitializeComponent();
            for(int i=0;i<vbook.GetUpperBound(0)+1;i++)
            {
                label1.Text = label1.Text + vbook[i]+Environment.NewLine;
            }            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty)                                                  //判断查找字符串是否为空
            {
                string[] cbook = Array.FindAll(vbook, (s) => s.Contains(textBox1.Text));        //使用findall的方法查找相应字符串
                if (cbook.Length > 0)                                                       //判断是否查找到相应字符串
                {
                    textBox2.Clear();
                    foreach (string strbook in cbook)                                               //向控件中添加字符串
                    {
                        textBox2.Text += strbook + Environment.NewLine;
                    }
                }
                else
                {
                    textBox2.Clear();
                    textBox2.Text = "没有该书目";
                }
            }
            else
            {
                textBox2.Clear();
            }
        }
    }
}

结语:

学会使用数组类Array中的FindAll方法查找数组中的相应信息。

喜欢的请关注和收藏!

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表