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.
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
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
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
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#中如何检查字符串数组中是否包含字符串数组中的特定工作?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号