String.Contains Method
Returns a value indicating whether the specified String object occurs within this string.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.String
The string to seek.
Return Value
Type: System.Booleantrue if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException |
value is null. |
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 */
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.
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
- 8/17/2010
- vijay_world
- 4/4/2012
- Bamara G. Coulibaly
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
- 1/5/2012
- Kaine
If myString.Contains("abc","def","ghi") Then
End If
A case-insensitivity option would be nice also.
- 11/30/2011
- nbrege
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) >= 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
- 11/7/2011
- vengeanz
- 11/7/2011
- R Petrusha - MSFT
- 10/28/2011
- BrianOr
bool contains = str1.IndexOf(str2, StringComparison.OrdinalIgnoreCase) >= 0;
- 8/20/2010
- Robert Horvick
- 10/19/2010
- dmex