3 out of 5 rated this helpful - Rate this topic

String.Contains Method

Returns a value indicating whether the specified String object occurs within this string.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public bool Contains(
	string value
)

Parameters

value
Type: System.String
The string to seek.

Return Value

Type: System.Boolean
true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.
Exception Condition
ArgumentNullException

value is null.

This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.

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


// 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);
    }
}
/*
This example produces the following results:

Is the string, s2, in the string, s1?: True
*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Why it doesn't support case ??? lower/upper !

Hi All,

If I tried following code...

string abc = "cn=63ARINDAMSSSEXCH2010DVARINDAMSSSRAHULEXCHDVVIJAYA1255567890QWER@A11.11";
            string def = "cn=63ArindamsssExch2010dvArindamsssRahulexchdvVijaya1255567890qwer";

if(abc.Contains(def))
{
Console.WriteLine("String is there buddy !");
}
Console.WriteLine("String is not there buddy !");


This returns me "String is not there buddy !" Why It doesn't support the IgnoreCase ...???

I know we can lower/upper both string; then can check... but it is not always feasible ...!

Any Idea of providing the ignore case ????

A Workaround, and Please Vote

if(abc.ToLower().Contains(def.ToLower()))
{
Console.WriteLine("String is there buddy !");
}
Console.WriteLine("String is not there buddy !");

Hope this Will Help

  public static class ExtensionMethods
{
public static bool ContainsString(string s1, string s2)
{
Console.WriteLine("Extension method");
return s1.ToLower().IndexOf(s2.ToLower())>=0;
}
}


Second Option use of contains method

  public static class ExtensionMethods
{
public static bool ContainsString(string s1, string s2)
{
Console.WriteLine("Extension method");
return s1.ToLower().Contains(s2.ToLower();
}
}

Bamara Coulibaly
MSc Software Engineering

Try the following String extension:

Hi,

Try the following extension method. I find it quick and intuitive to use:

/// <summary>
/// Extension methods for the <see cref="System.String"/> class.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Determines whether the source string contains the string value at some point. Comparison specifics
/// can be passed in to allow for culture specific comparisons.
/// </summary>
/// <param name="source">The source string in which to search.</param>
/// <param name="value">The string value to look for.</param>
/// <param name="comparison">Specifies the type of search to use for the specified string.</param>
/// <returns>
///  <c>true</c> if the specified source string contains the value string; otherwise, <c>false</c>.
/// </returns>
/// <remarks>Ideal for supporting multi-language and culture specific searches.</remarks>
public static bool Contains(this string source, string value, StringComparison comparison)
{
// Perform the comparison specific search.
if (value == null)
{
return false;
}
return source.IndexOf(value, comparison) >= 0;
}
}

Usage would be something like:

        bool isPresent = string1.Compare(string2, StringComparison.CurrentCultureIgnoreCase);


Hope it helps,

Kaine

Support multiple strings please
I would also like to see the .Contains method support multiple strings ie.

If myString.Contains("abc","def","ghi") Then

End If

A case-insensitivity option would be nice also.
Case sensitivity
@Ron, Could you make the StringComparison object an optional parameter to the extension method?

i.e.

public static class ExtensionMethods
{
public static bool ContainsString(this string s1, string s2, StringComparison comp = StringComparison.Ordinal)
{ Console.WriteLine("Extension method");
return (s1.IndexOf(s2, comp) &gt;= 0);
}
}

The StringComparison Parameter

Yes, you could. In fact, yours is a much more flexible method than the one I'd listed previously. Thanks for the post, vengeanz.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

Contains does not handle null values very well
It should be noted that the .contains cannot handle null so it is a good idea to check to see if the value you are checking is null IE:  if teststring  IsNot Nothing Then ...
Contains String[]
I'd love to see contains support Array   x.contains({"x","y","z"})

IndexOf for case insensitive
For case insensitive Contains consider using IndexOf:

bool contains = str1.IndexOf(str2, StringComparison.OrdinalIgnoreCase) >= 0;
Use LCase
Try using if(LCase(abc).Contains(def))