In this C# tutorial, I will explain, how to check if a C# string array contains substring with various methods. So let us check the “c# string array contains substring” with various examples.
C# string array contains substring
Now, let us check out, how to check if a C# string array contains a substring using various methods.
Method 1: Using Foreach Loop
The most straightforward method is to manually iterate over each string in the array and use the Contains
method to check for the substring in C#. We can simply use the foreach loop.
using System;
class Program
{
static void Main()
{
string[] usaNames = { "Washington", "California", "Texas", "New York", "Florida" };
string substringToCheck = "New";
foreach (var name in usaNames)
{
if (name.Contains(substringToCheck))
{
Console.WriteLine($"Found the substring '{substringToCheck}' in '{name}'.");
}
}
}
}
Once you run the code using visual studio console application, you can see the output below:
Method 2: Using LINQ
The Language Integrated Query (LINQ) library provides a more concise way to achieve this task. You can use the Any
method to check for the existence of a substring in any of the array elements in C#.
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] usaNames = { "Washington", "California", "Texas", "New York", "Florida" };
string substringToCheck = "New";
bool result = usaNames.Any(name => name.Contains(substringToCheck));
if (result)
{
Console.WriteLine($"The substring '{substringToCheck}' exists in the array.");
}
else
{
Console.WriteLine($"The substring '{substringToCheck}' does not exist in the array.");
}
}
}
Method 3: Using Array.Find
You can also use the Array.Find
method from the System
namespace in C#. This method returns the first element that matches the given condition.
using System;
class Program
{
static void Main()
{
string[] usaNames = { "Washington", "California", "Texas", "New York", "Florida" };
string substringToCheck = "New";
string foundName = Array.Find(usaNames, name => name.Contains(substringToCheck));
if (!string.IsNullOrEmpty(foundName))
{
Console.WriteLine($"Found the substring '{substringToCheck}' in '{foundName}'.");
}
else
{
Console.WriteLine($"The substring '{substringToCheck}' does not exist in the array.");
}
}
}
After I executed the code in Visual Studio, you can see the output below:
Conclusion
Checking for a substring within an array of strings in C# is a common but essential task. You can achieve this through various methods, such as using a simple loop, leveraging LINQ, or utilizing Array.Find
. I hope you got an idea about the topic, c# string array contains substring.
You may also like the following tutorials:
- Check if a String is Not Null or Empty in C#.NET
- How to check if an object is empty or null in C#.Net
- How to Convert a Number to a Comma-Separated String in C#?
- How to Check If String Contains a Character in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…