C#程序在字符串数组中搜索字符串
使用LinqContains()方法在字符串数组中搜索特定字符串。
string[] arr = { "Bag", "Pen", "Pencil"};现在,将字符串添加到字符串变量中,即要搜索的字符串。
string str = "Pen";
使用Contains()方法搜索上面的字符串。
arr.AsQueryable().Contains(str);
让我们看看整个例子。
示例
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
string[] arr = { "Bag", "Pen", "Pencil"};
string str = "Pen";
bool res = arr.AsQueryable().Contains(str);
Console.WriteLine("String Pen is in the array? "+res);
}
}输出结果
String Pen is in the array? True
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短