C# String Contains Method Example

In this source code example, we will demonstrate the usage of the String.Contains() method in C# with an example.

The C# String.Contains() method is used to return a value indicating whether the specified substring occurs within this string or not. If the specified substring is found in this string, it returns true otherwise false.

C# String Contains Method Example

The following example demonstrates the use of the Contains method with a substring.
using System;

public class StringDemo
{
    public static void Main(string[] args)
    {
        string s1 = "sourcecodeexamples";
        string s2 = "code";
        string s3 = "examples";
        
        Console.WriteLine(s1.Contains(s2));    
        
        Console.WriteLine(s1.Contains(s3));    

    }
}

Output:

True
True

Comments