C# String Contains

Description

String Contains returns a value indicating whether a specified substring occurs within this string.

Syntax

String.Contains has the following syntax.


public bool Contains(
  string value
)

Parameters

String.Contains has the following parameters.

  • value - The string to seek.

Returns

String.Contains method returns true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

Example

The following example determines whether the string "fox" is a substring of a familiar quotation.


//from   w  w  w. ja va2  s  .  c  om
// This example demonstrates the String.Contains() method 
using System;

class Sample 
{
    public static void Main() 
    {
        string s1 = "The quick brown fox jumps over the lazy dog";
        string s2 = "fox";
        bool b;
        b = s1.Contains(s2);
        Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version