System.String.Contains 方法

方法描述

返回一个值,该值指示指定的 String 对象是否出现在此字符串中。

语法定义(C# System.String.Contains 方法 的用法)

public bool Contains(
	string value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 要搜寻的字符串。
返回值 System.Boolean 如果 value 参数出现在此字符串中,或者 value 为空字符串 (""),则为 true;否则为 false。

提示和注释

此方法执行顺序(区分大小写和区域性)比较。 该搜索从此字符串的第一个字符位置开始,一直搜索到最后一个字符位置。

System.String.Contains 方法例子

下面的示例确定字符串“fox”是否为熟悉的引文中的子字符串。

// 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
*/

异常

异常 异常描述
ArgumentNullException value 为 null。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。