实现效果:
知识运用:
Array类的FindAll()方法,根据指定条件在数组中检索元素 返回一个包含匹配项的数组 无匹配项则返回空数组
public static T[] FindAll<T>(T[]array,Predicate<T>match)
array: 要搜索从零开始的的一维Array数组
match: Predicate<T>,定义要搜索的元素的条件;
实现代码:
string[] str_area; private void button1_Click(object sender, EventArgs e) { if(textBox1.Text!=string.Empty){ string[] arr_return = Array.FindAll (str_area,(s)=>s.Contains(textBox1.Text)); if (arr_return.Length > 0){ //判断找到 textBox2.Clear(); textBox2.Font = new Font("楷体", 15, FontStyle.Bold); foreach(string s in arr_return){ //遍历添加 textBox2.Text += s + Environment.NewLine; } } else { textBox2.Clear(); textBox2.Text = "没有找到"; } }else{ textBox2.Clear(); } } private void Form1_Load(object sender, EventArgs e) { str_area=new string[]{"湖北湖南","江西江淮","河北河南","山西陕西","长治长子"}; foreach(string str in str_area){ label2.Font = new Font("楷体",15,FontStyle.Bold); label2.Text += str + Environment.NewLine; } }