C# String Contains()

In C#, the Contains() method is used to determine whether a specified string or character sequence occurs within another string. It returns a Boolean value indicating whether the search string is found.

Here’s the syntax of the Contains() method:

public bool Contains(string value)

The Contains() method is called on an instance of the string where you want to perform the search. It takes a single parameter, which is the string you want to find within the target string. It returns true if the search string is found within the target string; otherwise, it returns false.

Here’s an example usage of the Contains() method:

string sentence = "The quick brown fox jumps over the lazy dog";
bool containsDog = sentence.Contains("dog");

if (containsDog)
{
    Console.WriteLine("The sentence contains the word 'dog'.");
}
else
{
    Console.WriteLine("The sentence does not contain the word 'dog'.");
}

In the above example, the Contains() method is used to check if the string “dog” is present in the sentence string. If it is found, the program will output “The sentence contains the word ‘dog’.” Otherwise, it will output “The sentence does not contain the word ‘dog’.”

Note that the Contains() method is case-sensitive. If you want to perform a case-insensitive search, you can use the IndexOf() method with the StringComparison.OrdinalIgnoreCase option or convert both strings to lowercase before performing the search.

Signature of C# String Contains() :

The signature of the Contains() method in C# for the System.String class is as follows:

public bool Contains(string value)

The Contains() method is used to determine whether a specified string occurs within the current string instance. It returns a Boolean value indicating whether the specified string is found or not.

Here’s an example usage of the Contains() method:

string sentence = "The quick brown fox jumps over the lazy dog.";

bool containsFox = sentence.Contains("fox");
Console.WriteLine(containsFox);  // Output: True

bool containsCat = sentence.Contains("cat");
Console.WriteLine(containsCat);  // Output: False

In the example above, the Contains() method is called on the sentence string to check if it contains the substring “fox”. It returns true because the string contains the specified value. On the other hand, when checking for the substring “cat”, it returns false since the string does not contain that value.

Parameters of C# String Contains() :

he signature of the String.Contains() extension method is as follows:

public static bool Contains(this string source, string value)

The String.Contains() method is used to determine whether a specified substring occurs within the given string. It returns a Boolean value indicating whether the specified substring is found or not.

Here’s an example usage of the String.Contains() extension method:

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string sentence = "The quick brown fox jumps over the lazy dog.";

        bool containsFox = sentence.Contains("fox");
        Console.WriteLine(containsFox);  // Output: True

        bool containsCat = sentence.Contains("cat");
        Console.WriteLine(containsCat);  // Output: False
    }
}

In the example above, the String.Contains() method is called on the sentence string to check if it contains the substring “fox”. It returns true because the string contains the specified substring. Conversely, when checking for the substring “cat”, it returns false since the string does not contain that substring.

Please note that the String.Contains() method is not a member of the System.String class itself, but an extension method defined in the System.Linq namespace. Therefore, you need to import the System.Linq namespace to use this method.

Return of C# String Contains() :

The String.Contains() method returns a Boolean value indicating whether a specified substring is found within the string or not. It returns true if the substring is found, and false otherwise.

Here’s an example usage of the String.Contains() method:

string sentence = "The quick brown fox jumps over the lazy dog.";

bool containsFox = sentence.Contains("fox");
Console.WriteLine(containsFox);  // Output: True

bool containsCat = sentence.Contains("cat");
Console.WriteLine(containsCat);  // Output: False

In the example above, the Contains() method is called on the sentence string to check if it contains the substring “fox”. Since the string does contain the specified substring, the method returns true. On the other hand, when checking for the substring “cat”, which is not present in the string, the method returns false.

C# String Contains() method Example:

Certainly! Here’s an example demonstrating the usage of the String.Contains() method in C#:

using System;

class Program
{
    static void Main()
    {
        string sentence = "The quick brown fox jumps over the lazy dog.";

        bool containsFox = sentence.Contains("fox");
        Console.WriteLine($"Contains 'fox': {containsFox}");  // Output: Contains 'fox': True

        bool containsCat = sentence.Contains("cat");
        Console.WriteLine($"Contains 'cat': {containsCat}");  // Output: Contains 'cat': False

        bool containsDog = sentence.Contains("dog");
        Console.WriteLine($"Contains 'dog': {containsDog}");  // Output: Contains 'dog': True

        bool containsEmpty = sentence.Contains("");
        Console.WriteLine($"Contains empty string: {containsEmpty}");  // Output: Contains empty string: True
    }
}

In this example, we have a sentence string that contains the phrase “The quick brown fox jumps over the lazy dog.” We use the Contains() method to check for the presence of various substrings.

  • containsFox checks if the string contains the substring “fox”, which is true.
  • containsCat checks if the string contains the substring “cat”, which is false.
  • containsDog checks if the string contains the substring “dog”, which is true.
  • containsEmpty checks if the string contains an empty string, which is true since any string contains an empty string.

The program then outputs the results indicating whether each substring is present or not.