Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Contains() - Method


Contains() Method


The Contains() Method is used to find a substring from the String.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        string x = "Beautiful";
    	bool y = x.Contains("tif");
    	
    	if (y == true) 
    	{ 
        	System.Console.WriteLine("The substring tif is present in the String"); 
        } 
        else 
        {
        	System.Console.WriteLine("The substring tif is not present in the String");
        }
    }
}


Output :



  The substring tif is present in the String

In the above code, we have declared a String Beautiful and assigned it to a variable x.


string x = "Beautiful";
C_Sharp


And we would be searching the substring tif, from the String, Beautiful.


So, we have used the Contains() Method to find if the substring tif is present in the String, Beautiful.


bool y = x.Contains("tif");

And what Contains() Method does is, goes to the String Beautiful and searches if the substring tif is present in it or not.

C_Sharp

And finds the substring substring tif is present.


And the below output is printed.

Output :



  The substring tif is present in the String