Part 32. String Contain()

Contain()
Is a string method. This method is used to check whether the given string occurs  or within a given string  there or not. if the string found it will return boolean value true or if string is not found it will return false.

public bool contains(string data)



  • if the data contain the string it will return true or if not contain value it return false.
Note : this Contain() method is used to searching data from start of the first character position given string and last index of the string.
 class Program
    {     
        static void Main(string[] args)
        {
            string str = "csharp-tutorial-point";
            String data = "csharp";
            System.Console.WriteLine(str.Contains(data));
            System.Console.ReadLine();        

        }

}

Output:

True

class Program

    {     
        static void Main(string[] args)
        {
            String  str = "csharp-tutorial-point";
            String  data = "csharp";
            bool val=str.Contains(data);
            System.Console.WriteLine(str+"  is in the string :-"+data,"Content Exists :"+ val);
            if (val)
            {
                int index = str.IndexOf(data);
                  if (index >=0)
                {
                    System.Console.WriteLine("'{0} begins at character position {1}",data, index + 1);
                }
            }

            System.Console.ReadLine();    
      }
}

Output

csharp-tutorial-point  is in the string :-csharp

'csharp begins at character position 1



Note: String is a 'str'  lowercase and you want to contain with an uppercase. in this case it will return false.

class Program
    {     
        static void Main(string[] args)
        {
              String  str = "csharp-tutorial-point";  //is a lower case
            if (str.Contains("CSHARP")==true)  //uppercase, it does't understand 
            {
                  System.Console.WriteLine("The string contain()");
            }
            else {
                  System.Console.WriteLine("The String does not Contains()");
            }    

            System.Console.ReadLine();  
            }
}


Output

The String does not Contains()

Next ..
33. String Remove()