Home » C#.Net

String.Contains() method with example in C#

C# String.Contains() method: Here, we are going to learn about the Contains() method of String class with example.
Submitted by IncludeHelp, on September 21, 2019

C# String.Contains() Method

String.Contains() method is used to check whether a given string contains a substring or not, we can use this method when we need to check whether a part of the string (substring) exists in the string.

Syntax:

    bool String.Contains(String substring);

The method is called with "this" string i.e. the string in which we have to check the substring.

Parameter:

  • substring – is the part of the string to be checked.

Return value:

  • bool – it returns "True" if substring exists in the string or it returns "False" if substring does not exist in the string.

Note: This method is case-sensitive.

Example:

    Input:
    string str  = "Hello world!";
    string str1 = "world";
    string str2 = "Hi";
    
    Function call:
    str.Contains(str1);
    str.Contains(str2);

    Output:
    True
    False

C# Example to convert string to characters array using String.Contains() method

Example 1:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string str = "Hello world!";
        string str1 = "world";
        string str2 = "Hi";

        // checking substring
        Console.WriteLine("str.Contains(str1): " + str.Contains(str1));
        Console.WriteLine("str.Contains(str2): " + str.Contains(str2));
    }
}

Output

str.Contains(str1): True
str.Contains(str2): False

Example 2:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string address = "102, Nehru Place, New Delhi, India.";
        string area1 = "Nehru Place";
        string area2 = "Sant Nagar";

        //checking and printing the result
        if (address.Contains(area1))
        {
            Console.WriteLine(area1 + " exists in the address " + address);
        }
        else
        {
            Console.WriteLine(area1 + " does not exist in the address " + address);
        }

        if (address.Contains(area2))
        {
            Console.WriteLine(area2 + " exists in the address " + address);
        }
        else
        {
            Console.WriteLine(area2 + " does not exist in the address " + address);
        }
    }
}

Output

Nehru Place exists in the address 102, Nehru Place, New Delhi, India.
Sant Nagar does not exist in the address 102, Nehru Place, New Delhi, India.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.