✨ Shield now has support for Avalonia UI

Check if a String Contains Another String in C#

Dec 30, 2023 | C#

Overview of String Manipulation in C#

Dive into the world of string manipulation with C#! You might be asking yourself, “why should I be interested in string manipulation?” Hopefully, this section will give you the answers.

The Importance of String Manipulation in Coding

Admit it, without a good understanding of string manipulation; it’s hard to get the most of C#. It’s like trying to paint a masterpiece without brushes; not impossible, but certainly more challenging! But don’t worry because it’s never too late to learn, is it?

Real-World Applications of String Manipulation

Have you ever wondered why your favorite app shows your name in the greeting? Or how Google displays instant search suggestions based on what you type? String manipulation plays a significant role here, simply put, it’s everywhere!

Understanding the Contains() Method in C#

Moving on, let’s dig deep into the Contains() method. Consider it a detective in our C# world, helping us find out if a string contains another string. Interesting, isn’t it?

Basic Syntax for Contains() Method

Here is the basic syntax for the Contains() method:

//YourString is the string you want to check  
//CheckString is the string you want to find
bool result = YourString.Contains(CheckString);

The function returns true if CheckString is found within YourString, and false if it is not. As simple as that!

Real-World Application of Contains() Method in C#

Imagine you’re developing a spell-checker app. Now, wouldn’t you need to check if the typed words are in your dictionary (string array) or not? There comes our Contains() method!

How to Check if A String Contains Another String in C#

Now that we know what Contains() does, let’s get our hands dirty with some c# coding, shall we? In the next couple of sections, we’ll go over the procedure step by step, and I’ll also show you some examples. Are you ready?

Step-by-Step Guide to Use Contains() Method

Here’s how you can find a string within another string:

string YourString = "Hello world!";
string CheckString = "world";
bool result = YourString.Contains(CheckString); // returns true

Pretty straightforward, right? If CheckString (“world”) is present in YourString(“Hello world!”), the result will be true.

Code Examples in C# to Check if a String Contains Another String

Let’s see another example:

string YourString = "Hello world!";
string CheckString = "universe";
bool result = YourString.Contains(CheckString); // returns false

In this case, since CheckString (“universe”) isn’t part of YourString (“Hello world!”), the result is false.

Checking String Arrays for String Presence in C#

But what if you have an array of strings rather than a single string? Do you need to iterate through each string in the array and use Contains()? Nope, because here we have another ace in the hole!

Essential Guidelines on How to Use the C# Check if a String Array Contains Another String

In C#, you can directly check if an array of strings contains a specific string. Here’s how you do it:

string[] YourStringArray = { "universe", "world", "earth" };
string CheckString = "world";
bool result = YourStringArray.Contains(CheckString); // returns true

It’s almost like the Contains() method for a single string, with the difference being that you apply it on an array.

Efficient Coding Techniques for Checking if One String Array Contains Another

Wondering about checking if one string array contains another array? It’s achievable with a few extra lines of code:

string[] YourStringArray = { "universe", "world", "earth" };
string[] CheckStringArray = { "world", "earth" };
bool result = !CheckStringArray.Except(YourStringArray).Any();  //returns true

If the CheckStringArray is a subset of YourStringArray, it returns true. Now, how cool is that?

Addressing Common Challenges When Checking Strings

We all know, regardless of the programming language, coding does come with its fair share of challenges. It’s those challenges that make coding worth it, right?

How to Troubleshoot Errors in Checking if String Contains Another String in C#

Errors can happen, but it’s about learning from those errors and improving. What happens if the string you’re checking is null, or if it contains different cases (uppercase vs. lowercase)? Fear not as these issues are easily addressable:

// Case-Insensitive Check  
bool result = YourString.Contains(CheckString, StringComparison.OrdinalIgnoreCase);

The above code can help you overcome the case sensitivity issue, and as for null strings, always use null checks before using Contains().

Congratulations on making this far!

The benefits of mastering this technique are considerable. It can play a vital role in data validation, search functionality, and so many more applications. It’s like a secret weapon, ready to deploy when the situation demands.

So, keep exploring, keep coding, and until next time, happy learning!

You May Also Like