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

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#中如何检查字符串数组中是否包含字符串数组中的特定工作?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435001.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
C# 程序读取字符串并计算所有数字的总和
上一篇 2025年12月17日 10:30:27
如何在 C# 中运行多个异步任务并等待它们全部完成?
下一篇 2025年12月17日 10:30:59

相关推荐

发表回复

登录后才能评论
关注微信