C# String Contains Substring

Checking for the presence of the specified substring in the string

In this tutorial, you will learn how to use the C# string Contains method.

The string Contains method checks whether a substring is contained in a string. If the substring is included, true is returned.

In string manipulation, there are often situations where you want to search for a specific string. There are several types of search methods, and C# and other languages even provide generic methods. Since this is the basis of data processing, it is definitely one of the things you want to learn.

The syntax of the C# string Contains method is as follows:

string.Contains(string value)

C# String Length

Example:

  1. string text = "Let's learn C# programming online!";
  2. string search_text = "programming";
  3. bool exist = text.Contains(search_text);

C#'s Contains method is used to check whether a given string is contained in the target string; unlike the IndexOf method, it does not return the specified position, but it should be sufficient if all you want to do is check for the presence of a given string. Note that this method is available for String and List types but not for array types. However, it is possible to do so using Linq's Contains method.

The argument value specifies the character or substring to be checked for existence. Returns true if the specified substring is included, false otherwise.

The method string Contains performs a case-sensitive ordinal comparison. In other words, it is equivalent to calling the String IndexOf method with StringComparison.Ordinal and checking if the return value is greater than or equal to zero.

The Contains method returns true if a whitespace string is specified in the string Contains().

C# string Contain method Example

Example 1:

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string text = "Online C# programming tutorials.";
  9. string search_string = "programming";
  10. bool exist = text.Contains(search_string);
  11. Console.WriteLine("Returned value: {0}", exist);
  12. }
  13. }
  14. }
  15.  

This sample code returns whether the substring to be searched for is contained within the specified string.

Output:

Returned value: True

Example 2:

The following sample code illustrates case-sensitive searching in the string Contain method.

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string text = "Let's learn C# programming online!";
  9. string search_string = "Programming";
  10. bool exist = text.Contains(search_string);
  11. if (exist)
  12. {
  13. Console.Write("\"{0}\" is included in \"{1}\"", search_string, text);
  14. }
  15. else
  16. {
  17. Console.Write("\"{0}\" is not included in \"{1}\"", search_string, text);
  18. }
  19. }
  20. }
  21. }

Output:

"Programming" is not included in "Let's learn C# programming online!"

Summary

In this tutorial, you learned how to use the string Contain method. The string Contain method checks whether a given substring is contained in the string.

For more tutorials on C#, please see this page.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables