System.String.StartsWith 方法 (String)

方法描述

确定此字符串实例的开头是否与指定的字符串匹配。

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

public bool StartsWith(
	string value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 要比较的字符串。
返回值 System.Boolean 如果 value 与此字符串的开头匹配,则为 true;否则为 false。

提示和注释

此方法将 value 与位于此实例开头、与 value 长度相同的子字符串进行比较,并返回它们是否相等的指示。 若要相等,value 必须是空字符串 (String.Empty)、必须对此同一实例的引用,或者必须与此实例的开头匹配。

此方法使用当前区域性执行单词(区分大小写和区域性)比较。

对调用者的说明

如 在 .NET Framework 中使用字符串的最佳做法 中所述,我们建议您避免调用替换默认值的字符串比较方法,而是调用需要显式指定参数的方法。 若要使用当前区域性的字符串比较规则确定字符串是否以特定子字符串开头,请为它的 comparisonType 参数使用 StringComparison.CurrentCulture 的值调用 StartsWith(String, StringComparison) 方法重载。

System.String.StartsWith 方法 (String)例子

下面的示例演示如何使用 StartsWith 方法。

using System;

public class EndsWithTest {
    public static void Main() {

        // process a string that contains html tags
        // this sample does not remove embedded tags (tags in the middle of a line)

        string [] strSource = { "This is bold text", "

This is large Text

", "This has multiple tags", "This has embedded tags.", "" ); // remove the identified section, if it is a valid region if ( lastLocation >= 0 ) item = item.Substring( lastLocation + 1 ); } return item; } }

异常

异常 异常描述
ArgumentNullException value 为 null。

命名空间

namespace: System

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.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 系统要求。