卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

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++中,以O(n)的时间复杂度和O(1)的额外空间重新排列正负数
下一篇: 如何在 C# 中运行多个异步任务并等待它们全部完成?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏