C# program to check if a Substring is present in a Given String

CsharpServer Side ProgrammingProgramming

Use the contains() method in C# to check if a substring is in a given string.

Let us say the string is −


Within the string, you need to find the substring “Uni”. For that, use the contains method and use it like the following code snippet −

res = str1.Contains(str2);


You can try to run the following code to find a substring in a string.

Live Demo

using System;
public class Demo {
   public static void Main() {
      string str1 = "United", str2 = "Uni";
      bool res;
      res = str1.Contains(str2);
      if (res)
         Console.Write("The substring " + str2 + " is in the string " + str1);
         Console.Write("The substring " + str2 + " is not in the string " + str1);


The substring Uni is in the string United
Updated on 19-Jun-2020 08:30:11