How to check if a string array contains a specific job in a string array in C#?

WBOY
Release: 2023-08-27 11:17:10
forward
1171 people have browsed it

How to check if a string array contains a specific job in a string array in 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();
}
Copy after login

输出

String Not Present
Copy after login

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();
}
Copy after login

输出

String Present
Copy after login

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();
}
Copy after login

输出

Sachin
Copy after login

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();
}
Copy after login

输出

String is present
Copy after login

The above is the detailed content of How to check if a string array contains a specific job in a string array in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template