C#中如何检查字符串数组中是否包含字符串数组中的特定工作?

2023年 8月 27日 46.0k 0

C#中如何检查字符串数组中是否包含字符串数组中的特定工作?

In C#, String.Contains() is a string method. This method is used to check whether the substring occurs within a given string or not.

It returns the boolean value. If substring exists in string or value is the empty string (“”), then it returns True, otherwise returns False.

Exception − This method can give ArgumentNullException if str is null.

This method performs the case-sensitive checking. The search will always begin from the first character position of the string and continues until the last character position.

Example 1

Contains is case sensitive if the string is found it return true else false

static void Main(string[] args){
string[] strs = { "Sachin", "India", "Bangalore", "Karnataka", "Delhi" };
if (strs.Contains("sachin")){
System.Console.WriteLine("String Present");
} else {
System.Console.WriteLine("String Not Present");
}
Console.ReadLine();
}

登录后复制

输出

String Not Present

登录后复制

Example 2

static void Main(string[] args){
string[] strs = { "Sachin", "India", "Bangalore", "Karnataka", "Delhi" };
if (strs.Contains("Sachin")){
System.Console.WriteLine("String Present");
} else {
System.Console.WriteLine("String Not Present");
}
Console.ReadLine();
}

登录后复制

输出

String Present

登录后复制

Example 3

的中文翻译为:

示例 3

static void Main(string[] args){
string[] strs = { "Sachin", "India", "Bangalore", "Karnataka", "Delhi" };
var res = strs.Where(x => x == "Sachin").FirstOrDefault();
System.Console.WriteLine(res);
Console.ReadLine();
}

登录后复制

输出

Sachin

登录后复制

Example 4

的中文翻译为:

示例4

static void Main(string[] args){
string[] strs = { "Sachin", "India", "Bangalore", "Karnataka", "Delhi" };
foreach (var item in strs){
if (item == "Sachin"){
System.Console.WriteLine("String is present");
}
}
Console.ReadLine();
}

登录后复制

输出

String is present

登录后复制

以上就是C#中如何检查字符串数组中是否包含字符串数组中的特定工作?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论